フィルターのクリア

Nested if statement not perusing all if statements

2 ビュー (過去 30 日間)
Jay
Jay 2015 年 6 月 25 日
コメント済み: Jay 2015 年 6 月 25 日
matrixA = rand(100,2)
matrixB = zeros(100,1)
a= 78.6
b = 90.6
c = 99.5
d = 156.1
for i = 1:100
if matrixA(i,2) < 0
matrixB(i,1) = i/a*i
else if matrixA(i,1) >=0 <= a
matrixB(i,1) = b/c
else if matrixA(i,1) > a <= b
matrixB(i,1) = a/d*5
else if matrixA(i,1) > c <=d
matrixB(i,1) = a*d*c
else if martixA(i,1) >d
matrixB(i,1) = 78/b*d
end
end
end
end
end
end
I cant get the code to peruse through all the else if statements. It will only peruse the first else if statement then skip the rest.

採用された回答

Mischa Kim
Mischa Kim 2015 年 6 月 25 日
編集済み: Mischa Kim 2015 年 6 月 25 日
Justin, almost there...
for i = 1:100
if matrixA(i,2) < 0
matrixB(i,1) = i/a*i
elseif matrixA(i,1) >=0 && matrixA(i,1) <= a
matrixB(i,1) = b/c
elseif matrixA(i,1) > a && matrixA(i,1) <= b
matrixB(i,1) = a/d*5
elseif matrixA(i,1) > c && matrixA(i,1) <= d
matrixB(i,1) = a*d*c
elseif martixA(i,1) > d
matrixB(i,1) = 78/b*d
end
end
  • elseif is one word.
  • Two conditions (e.g. range of values) need to be combined with a logical &&
  1 件のコメント
Jay
Jay 2015 年 6 月 25 日
Thanks for that Mischa.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeControl Flow についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by