how to write if statement for matrix ?
35 ビュー (過去 30 日間)
古いコメントを表示
how to write if statement for matrix ?
in other words:
test= [5;6;0;-1;0]
this is the condition:
if test==0
disp 0
else
disp 5
end
and I want to give answer for each row (for 5 and 6 and 0 ....etc)
0 件のコメント
回答 (1 件)
Geoff Hayes
2020 年 5 月 16 日
Ibrahim - what are you trying to do here? Just display (with disp) a message depending upon whether an element is a zero or not? The simplest and least efficient way to do this is with a loop
test = [5;6;0;-1;0];
for k = length(test)
if test(k) == 0
disp 0;
else
disp 5;
end
end
I don't think that is what you really want though so you may need to provide more details. I also suspect that you shouldn't need to use a for loop and that may be the case depending upon the details you provide.
2 件のコメント
Geoff Hayes
2020 年 5 月 18 日
Is the output array of the same dimensions as test?
test = [5;6;0;-1;0];
outputArray = size(test);
for k = length(test)
if test(k) == 0
outputArray(k) = 0;
else
% do a calculation of some kind
outputArray(k) = 42; % <--- your code here
end
end
参考
カテゴリ
Help Center および File Exchange で Multidimensional Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!