Trying to do repeat math on multiple variables...

2 Ansichten (letzte 30 Tage)
Spencer Tully
Spencer Tully am 17 Mär. 2018
Kommentiert: Stephen23 am 18 Mär. 2018
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 Kommentar
Stephen23
Stephen23 am 18 Mär. 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:

Melden Sie sich an, um zu kommentieren.

Antworten (2)

David Fletcher
David Fletcher am 17 Mär. 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)?

Stephen23
Stephen23 am 18 Mär. 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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by