Loop over an array or list of vectors
780 views (last 30 days)
Show older comments
skrowten_hermit
on 9 Oct 2020
Commented: Walter Roberson
on 28 May 2022
Is there a way to loop over an array of signal vectors in Matlab? Python has a way to do it using for loop like:
v1 = [] //signal vector1
v2 = [] //signal vector2
arrv = [v1, v2] //array of signal vectors
for v in arrv:
print len(v)
It would be great if someone can suggest a similar solution if it exists.
2 Comments
Sudhakar Shinde
on 9 Oct 2020
Could you specifically mention input types of vector?
If you would like to get length of array, below snap could be very small and beginer example:
Accepted Answer
Stephen23
on 9 Oct 2020
Edited: Stephen23
on 9 Oct 2020
While it is possible to loop over array elements directly, in practice it is usually much more convenient and versatile to loop over their indices. You can use a cell array and loop over its indices:
C = {v1, v2}; % cell array
for k = 1:numel(C) % indices
v = C{k};
... do whatever with v
end
8 Comments
Stephen23
on 22 Dec 2021
Edited: Stephen23
on 28 May 2022
"Could you elaborate a bit more on why it is more convenient to loop over the indices?
Working with array sizes/indices makes it trivial to preallocate any output arrays, to access the input array using indexing, and then to store any outputs using indexing, which covers 99% of the time when working with loops. The other 1% happen so rarely that I certainly would even bother to start writing a loop to iterate over the data, because I know that in just a few moments I will want to add to the loop some output arrays and more indexing... which is simpler if the iterator uses indices, not data.
"I personally think that it is more clean to loop over array element directly (like in python with the "in" operator,) but probably I am missing something here."
MATLAB is not Python.
With Python you can use enumerate to loop over the values and generate indices at once. MATLAB does not really offer this feature (well... it does: you could loop over the columns of an input matrix but all readers of your code in future will dislike your code intensely and use lots of bad words to describe it. I have never seen anyone intentionally use that feature of MATLAB).
Walter Roberson
on 28 May 2022
I have looped over columns a quite small number of times. There isn't much call for it.
More Answers (1)
Ameer Hamza
on 9 Oct 2020
Edited: Ameer Hamza
on 9 Oct 2020
Yes, you can loop over an array using for loop
v1 = ; % signal 1
v2 = ; % signal 2
arrv = [v1, v2] % array of signal vectors
for i = arrv % arrv must be a row vector
i % takes values from arrv one at a time
end
1 Comment
Stephen23
on 9 Oct 2020
Edited: Stephen23
on 9 Oct 2020
That answer is not equivalent to the Python code shown in the question. In Python
arrv = [v1, v2]
creates a list with two elements, where arrv[0] == v1 and arrv[1] == v2.
In contrast this code in MATLAB
arrv = [v1, v2]
attempts to concatenate v1 and v2 horizontally, which if successful will result in one single array. The for operator then loops over the columns of that array, which if v1 and v2 are column vectors may give the same as Python, but if they are row vectors will give a very different result. I have never seen good code that relies on that strange for "feature".
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!