Loop over an array or list of vectors

780 views (last 30 days)
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
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:
skrowten_hermit
skrowten_hermit on 9 Oct 2020
Just about anything. For example, to read a set of audio files like using the function audioread():
wav1 = "example1.wav"
wav2 = "example2.wav"
wav3 = "example3.wav"
wavarr = [wav1, wav2, wav3]
and loop over them to calculate fft of the above signal vectors.
Stephen Cobeldick's answer below should be able to do it right?

Sign in to comment.

Accepted Answer

Stephen23
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
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
Walter Roberson on 28 May 2022
I have looped over columns a quite small number of times. There isn't much call for it.

Sign in to comment.

More Answers (1)

Ameer Hamza
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
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".

Sign in to comment.

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!