New Vector

5 ビュー (過去 30 日間)
Jesse
Jesse 2012 年 3 月 20 日
I am trying to write a for loop and generate a new vector. What notation do I need for the Syb(?) in the following code?
Syb = [];
for m =1:length(I_FinalOut)
for n=1:length(Q_FinalOut)
if I_FinalOut(m) > 0 && Q_FinalOut(n) > 0
Syb(?) = 0
elseif I_FinalOut(m) < 0 && Q_FinalOut(n) > 0
Syb(?) = 1
elseif I_FinalOut(m) > 0 && Q_FinalOut(n) < 0
Syb(?) = 2
elseif I_FinalOut(m) < 0 && Q_FinalOut(n) < 0
Syb(?) = 3
end
end
end
If I use (i), then it goes through my I_FinalOut, and if I use (j) it does the Q_FinalOut. So I am not sure what notation to use to get a new vector Syb of it's own based on the if statement.

回答 (1 件)

Aldin
Aldin 2012 年 3 月 20 日
Hi, i think you need 4 counters, in this case you code will be like this:
Syb = []; counter1 = 0; counter2 = 0; counter3 = 0; counter4 = 0; for m =1:length(I_FinalOut)
for n=1:length(Q_FinalOut)
if I_FinalOut(m) > 0 && Q_FinalOut(n) > 0
counter1 = counter1 + 1;
Syb(counter1) = 0
elseif I_FinalOut(m) < 0 && Q_FinalOut(n) > 0
counter2 = counter 2 + 1;
Syb(Syb(end)+counter2) = 1
elseif I_FinalOut(m) > 0 && Q_FinalOut(n) < 0
counter3 = counter3 + 1;
Syb(Syb(end)+counter3) = 2
elseif I_FinalOut(m) < 0 && Q_FinalOut(n) < 0
counter4 = cpunter4 + 1;
Syb(Syb(end)+counter4) = 3
end
end
end
or other solution if only on if condition is ready for processing (true):
Syb = []; counter1 = 0; counter2 = 0; counter3 = 0; counter4 = 0; for m =1:length(I_FinalOut)
for n=1:length(Q_FinalOut)
if I_FinalOut(m) > 0 && Q_FinalOut(n) > 0
counter1 = counter1 + 1;
Syb(counter1) = 0
elseif I_FinalOut(m) < 0 && Q_FinalOut(n) > 0
counter2 = counter 2 + 1;
Syb(counter2) = 1
elseif I_FinalOut(m) > 0 && Q_FinalOut(n) < 0
counter3 = counter3 + 1;
Syb(counter3) = 2
elseif I_FinalOut(m) < 0 && Q_FinalOut(n) < 0
counter4 = cpunter4 + 1;
Syb(counter4) = 3
end
end
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