Loop over an array or list of vectors

181 visualizaciones (últimos 30 días)
skrowten_hermit
skrowten_hermit el 9 de Oct. de 2020
Comentada: Walter Roberson el 28 de Mayo de 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 comentarios
Sudhakar Shinde
Sudhakar Shinde el 9 de Oct. de 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 el 9 de Oct. de 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?

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 9 de Oct. de 2020
Editada: Stephen23 el 9 de Oct. de 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 comentarios
Stephen23
Stephen23 el 22 de Dic. de 2021
Editada: Stephen23 el 28 de Mayo de 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 el 28 de Mayo de 2022
I have looped over columns a quite small number of times. There isn't much call for it.

Iniciar sesión para comentar.

Más respuestas (1)

Ameer Hamza
Ameer Hamza el 9 de Oct. de 2020
Editada: Ameer Hamza el 9 de Oct. de 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 comentario
Stephen23
Stephen23 el 9 de Oct. de 2020
Editada: Stephen23 el 9 de Oct. de 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".

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by