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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by