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 Center および File ExchangeCall 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