複数条件の繰り返し分( for, if)

192 vues (au cours des 30 derniers jours)
Kouki
Kouki le 1 Nov 2019
Commenté : Kenta le 3 Nov 2019
"~かつ~" といった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を使って表現したいです.

Réponse acceptée

Kenta
Kenta le 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 commentaires
Kouki
Kouki le 2 Nov 2019
回答ありがとうございます.
こちらを参考にさせていただきたいと思います.
Kenta
Kenta le 3 Nov 2019
お返事ありがとうございます。解決し、よかったです。

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!