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

 채택된 답변

Image Analyst
Image Analyst 2022년 2월 10일

0 개 추천

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
ali
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
ali

댓글 수: 5

ali hassan
ali hassan 2022년 2월 10일
@Image Analyst what if i want to know that how many times my first condition is fullfilled or you can say how many times it happened that:
% any element in array A is 5 and the corresponding B element is between 2 and 6
can it be done without using loop?
Image Analyst
Image Analyst 2022년 2월 10일
The conditions are logical variables that can be converted to 0 or 1 when you do math on them. So to count the number of times in the vector where the condition is true, you can use sum
count = sum(A==5 & B>=2 & B<=6)
ali hassan
ali hassan 2022년 2월 11일
@Image Analyst what if i want it to give logic 1 only if the condition satisfies for 5 times in a row. likewhat i mean is that it continuosly repeat for 5 times.
A=[1 5 5 5 5 5 6];
B=[6 2 5 5 4 3 5];
here only once it is happening that condiition is fulfilled five times in a row.
ali hassan
ali hassan 2022년 2월 11일
here we have a vector column 'FF_liter_ho_'.If i want to know that how many times non zero values were greater than 770 in a row.
so it happened only once in the column.
@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]
ff_liter_ho = 1×18
NaN 75 0 NaN NaN NaN NaN NaN NaN NaN 75 37 NaN 0 1 2 3 4
nonZeroIndexes = ff_liter_ho > 0
nonZeroIndexes = 1×18 logical array
0 1 0 0 0 0 0 0 0 0 1 1 0 0 1 1 1 1
props = regionprops(nonZeroIndexes, 'Area');
numRuns = length(props)
numRuns = 3
runLengths = [props.Area]
runLengths = 1×3
1 2 4

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2022년 2월 7일

0 개 추천

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

KSSV
KSSV 2022년 2월 7일
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
KSSV 2022년 2월 7일
Any example data?
ali hassan
ali hassan 2022년 2월 10일
assume, i have an array
A=[1 2 5 66 7] and B=[6 4 4 77 8]
i want to put if condition such that if any element in array A is 5 and the corresponding is between 2 and 6, i want to know how many times the condition meets.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

질문:

2022년 2월 7일

댓글:

2022년 2월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by