Trying to do repeat math on multiple variables...
2 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 18 Mar 2018
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:
Answers (2)
David Fletcher
on 17 Mar 2018
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 Comments
Stephen23
on 18 Mar 2018
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 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!