for loop value array must be a row vector(?)
Mostrar comentarios más antiguos
Hi all,
The next-to-simplest matlab code is approximately:
idxs = [1,2,3];
for k = idxs
k
end
But if the idxs array is a column vector, the results are rather different:
idxs = [1;2;3];
for k = idxs
k
end
That is, you get one value of k equal to the whole column vector. I did not know that. I've been using Matlab for almost 30 years. How did I miss that?
2 comentarios
This is an ancient MATLAB feature that no one uses, no one likes, and only causes bugs:
I for one would love to be able to write:
for k = find(..)
without the ugly rigmarole and obfuscation that is currently required.
No doubt some at TMW worry about "breaking existing code", but that argument does not hold much water:
- no one uses this syntax
- it causes far more bugs than it being used as intended,
- TMW have broken other fairly fundamental syntaxes (e.g. input/output argument order of some basic functions, changed default first/last value returned, changes to scoping rules, etc. etc.) without causing the end of the world.
Just like IF accepting non-scalar values, this is something that should have been retired a long time ago.
Walter Roberson
el 17 de Mayo de 2023
I have occasionally taken advantage of this aspect of for. Not very much at all.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Whos 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!