Loop over an array or list of vectors

조회 수: 181 (최근 30일)
skrowten_hermit
skrowten_hermit 2020년 10월 9일
댓글: Walter Roberson 2022년 5월 28일
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
Sudhakar Shinde
Sudhakar Shinde 2020년 10월 9일
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 2020년 10월 9일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2020년 10월 9일
편집: Stephen23 2020년 10월 9일
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
Stephen23
Stephen23 2021년 12월 22일
편집: Stephen23 2022년 5월 28일
"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 2022년 5월 28일
I have looped over columns a quite small number of times. There isn't much call for it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 10월 9일
편집: Ameer Hamza 2020년 10월 9일
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
Stephen23
Stephen23 2020년 10월 9일
편집: Stephen23 2020년 10월 9일
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".

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by