Trying to do repeat math on multiple variables...
2 次查看(过去 30 天)
显示 更早的评论
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
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
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)?
0 个评论
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!