This is my code but I'm sure MATLAB provides a more elegant way to do this. Any thoughts?

all = csvread('TRAINING_i.csv');
actual = csvread('TRAINING_INPUTS.csv');
indTraining = zeros(size(actual,1),1);

for i = 1:size(actual,1)

    indTraining(i,1) = find(ismember(all, actual(i,:), 'rows'));

end
有帮助吗?

解决方案

I don't know if I follow exactly but I think this is what you're trying to do:

A = [1 2; 
     3 4; 
     5 6; 
     7 8];

B = [3 4; 
     7 8];

for i = 1:size(B,1)

    indTraining(i,1) = find(ismember(A, B(i,:), 'rows'));

end

indTraining now is [2, 4]. This is more easily achieved as follows:

[~, indTraining] = ismember(B, A, 'rows')

No loops needed nor find. If you find yourself using find on a common function in Matlab, it's worth checking the docs on that function first because the second or third output to many common functions is often the indexes of what ever the function did and will save you the trouble e.g. second output of max etc

Lastly don't use all as a variable name in matlab because you're masking a very useful function.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top