複数条件の繰り返し分( for, if)
331 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
"~かつ~" といった2つの条件を満たす繰り返しの文を表現したいです.
A=[1 50;2 51;1 53;2 55;1 56;2 52]
P1=55
P2=52
【条件】
*Aの1列目が"1"かつ,P1<=Aの2列目 この2つの条件を満たすとき⇒ "1"と表示
満たさないとき⇒ "0"と表示
*Aの1列目が"2"かつ,P2<=Aの2列目 この2つの条件を満たすとき⇒ "2"と表示
満たさないとき⇒ "0"と表示
B=[0 0 0 2 1 2]
表示したものを上の様にBに数値として格納
これらをforを使って表現したいです.
0 Kommentare
Akzeptierte Antwort
Kenta
am 2 Nov. 2019
clear;clc
A=[1 50;2 51;1 53;2 55;1 56;2 52];
P1=55;
P2=52;
B=zeros(size(A,1),1);
for i=1:size(A,1)
if A(i,1)==1&&P1<=A(i,2)
B(i,1)=1;
elseif A(i,1)==2&&P2<=A(i,2)
B(i,1)=2;
else
B(i,1)=0;
end
end
B
これでいかがでしょうか。
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!