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!