Sm1
Sm1

Reputation: 570

Matlab : Confusion over find() function

find() function returns the indices where the elements are non-zero. I tried with different array sizes but both give error :

In an assignment  A(I) = B, the number of elements in B and I must be the same. 

I am confused because when the array size is same, still I am getting this error.

This is just to understand what went wrong: LEt,

Example 1: Same array size

A = [20;21;3;45;5;19;1;8;2;1];
B = A;
 for i =1:length(B)
pos(i) = find(A == B(i));
end

I should have got pos = [1,2,3,4,5,6,7,8,9,10]. But the loops exits after i = 7, giving `pos = [1,2,3,4,5,6]'

Example 2: Dissimilar array size

 C = [20;1;10;3];

 for i =1:length(C)
    pos(i) = find(A == C(i));
    end

Can somebody please explain what is wrong in my understanding and an illustration of how I can work with same and different array length of A and B? Thank you.

Upvotes: 0

Views: 34

Answers (1)

optisimon
optisimon

Reputation: 136

The problem is that find(A == 1) returns two indexes, both 7 and 10, and that can't be stored in pos(i), since pos(i) can only hold a single number.

Unfortunately, the generic error message happened to have the same name for the matrices as two of your matrices, which can be confusing before you'we seen it a few times.

Upvotes: 1

Related Questions