if loop on array
7 views (last 30 days)
Show older comments
ali hassan
on 7 Feb 2022
Commented: Image Analyst
on 11 Feb 2022
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
0 Comments
Accepted Answer
Image Analyst
on 10 Feb 2022
Your parentheses are not right. Try it this way
a=[3 4 3 44 3];
b=[4 3 4 34 26];
if any(a==3 & b==4)
fprintf('ali\n')
end
A=[1 2 5 66 7];
B=[6 4 4 77 8];
% if any element in array A is 5 and the corresponding B element is between 2 and 6
if any(A==5 & B>=2 & B<=6)
fprintf('ali')
else
fprintf('No Matches.\n')
end
5 Comments
Image Analyst
on 11 Feb 2022
@ali hassan if you have the Image Processing Toolbox, you can use regionprops():
ff_liter_ho = [nan, 75, 0, nan, nan, nan, nan, nan, nan, nan, 75, 37, nan, 0, 1, 2, 3, 4]
nonZeroIndexes = ff_liter_ho > 0
props = regionprops(nonZeroIndexes, 'Area');
numRuns = length(props)
runLengths = [props.Area]
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
See Also
Categories
Find more on Matrix Indexing 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!