if loop on array
Show older comments
i want to use if loop on a array. let say if i have two arrays:
a=[1 2 3 4 5]
b=[2 3 4 5 6]
now if i want to use if loop such that when any value in array 'a' is 3 and corresponding value in array 'b' is 4,it should print 'ali'.
i tried the following code but it did'nt work.
a=[3 4 3 44 3];
b=[4 3 4 34 26];
if (any(a==3) & b==4)
sprintf('ali')
end
Accepted Answer
More Answers (1)
KSSV
on 7 Feb 2022
a=[3 4 3 44 3];
b=[4 3 4 34 26];
if (any(a==3))
idx = a == 3 ;
if any(b(idx) == 4 )
fprintf('ali\n')
end
end
4 Comments
KSSV
on 7 Feb 2022
@ali hassan commented: https://www.mathworks.com/matlabcentral/answers/1644540-if-loop-on-array#answer_890440
what if i want to give a range of valus? like if a has a value between 3 and 4 and corresponding value of b is between 4 and 5?
KSSV
on 7 Feb 2022
Any example data?
ali hassan
on 10 Feb 2022
ali hassan
on 10 Feb 2022
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!