Trying to do repeat math on multiple variables...

2 ビュー (過去 30 日間)
Spencer Tully
Spencer Tully 2018 年 3 月 17 日
コメント済み: Stephen23 2018 年 3 月 18 日
Fs = 44100;
W_t1 = [0,150]; W_r1 = [0,0];
W_t2 = [150,600]; W_r2 = [0,0];
W_t3 = [600,1700]; W_r3 = [0,0];
W_t4 = [1700,7000]; W_r4 = [0,0];
W_t5 = [7000,22050]; W_r5 = [0,0];
%W_r1 = W_t1 * 2*pi/Fs;
for i = [1:5]
W_ri = W_ti*(2*pi/Fs) % This is where I want the loop to preform that math for W_t(1-5)
end
  1 件のコメント
Stephen23
Stephen23 2018 年 3 月 18 日
Note that looping over lots of separate variable names is slow, complex, buggy, hard to debug, and is not recommended by the MATLAB documentation: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
As David Fletcher showed and the MATLAB documentation reccomends, the best solution is to simply put all of your data into one numeric array, because this makes processing it trivially simple and very efficient. Accessing data using indexing is very efficient.
Read more here:

サインインしてコメントする。

回答 (2 件)

David Fletcher
David Fletcher 2018 年 3 月 17 日
Fs = 44100;
W_t = [0,150;150,600;600,1700;1700,7000;7000,22050];
W_r = W_t.*(2*pi/Fs)
Is that what you wanted to do (more or less)?

Stephen23
Stephen23 2018 年 3 月 18 日
David Fletcher already showed the best way of doing this, which is to use MATLAB efficiently with one numeric array. If you really want to loop over lots of separate arrays then use cell arrays and indexing:
W_t{1} = [0,150];
W_t{2} = [150,600];
W_t{3} = [600,1700];
W_t{4} = [1700,7000];
W_t{5} = [7000,22050];
W_r = cell(size(W_t));
for k = 1:numel(W_t)
W_r{k} = W_t{k} * 2*pi/Fs;
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by