In an assignment A(I) = B, the number of elements in B and I must be the same.
Mostrar comentarios más antiguos
a=[];
b=[];
for i=1:length(l)
[m n]=find(R==l(i));
a(i)=m;
b(i)=n;
end
In an assignment A(I) = B, the number of elements in B and I must be the same.
In this code l is a column vector and R a matrix containing all elements of l and more.This 'for loop' is contained in yet another 'for loop'.Now for the first couple loops in the bigger 'for loop' no error is given.Then halfway through, the error shown above is displayed. I cannot understand why this happens, and why the error is not given all the time.
Respuesta aceptada
Más respuestas (2)
Image Analyst
el 15 de En. de 2012
That's NOT going to give you what you think it will. Your index will ALWAYS be either empty or just one. You're getting the error when it's empty (no match). It's not going to give you a list of indexes where l matches R. Even if you do put in a check for empty, your a and b will just be 1 or 0, not the indices.
Simply say
[a b] = find(R == l);
and do away with the for loop altogether, and get the indices like you want.
Or if you really want a binary comparison (match or no match), simply do
binaryMap = l == R;
Peter
el 15 de En. de 2012
0 votos
1 comentario
Image Analyst
el 15 de En. de 2012
Exactly what I said. I wrote up a little demo and tested it and that's how I discovered the "empty" problem before I answered. I'm not sure how you used cyclists answer to resolve your problem or why you still chose to do it that way instead of the "isempty()" way or "loopless/vectorized" way I suggested, but anyway, for small loops it shouldn't matter much.
Categorías
Más información sobre Function Creation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!