How can I make if command's statements intersect and after that receive the result. For example the answer for this code is 'noyesno' ....but I want it to be no, cause not all the elements of a and b are equal.
n=3
a=[0 1 2]
b=[1 1 3]
i=1
for i=1:n
if a(i)==b(i)
fprintf ('yes')
else
fprintf ('no')
end
end

 채택된 답변

Alex Mcaulley
Alex Mcaulley 2020년 1월 21일

1 개 추천

a=[0 1 2];
b=[1 1 3];
if all(a==b)
fprintf ('yes')
else
fprintf ('no')
end

추가 답변 (1개)

Bhaskar R
Bhaskar R 2020년 1월 21일
편집: Bhaskar R 2020년 1월 21일

1 개 추천

n=3; % length of the your vector i guess
a=[0 1 2];
b=[1 1 3];
% depends on n value
is_intersect = sum(a(1:n)==b(1:n))==n;
% if a and b vectors are same length
is_intersect = sum(a==b)==length(a);
if is_intersect
fprintf ('yes');
else
fprintf ('no');
end
Hope i got you !!

댓글 수: 1

Ani Asoyan
Ani Asoyan 2020년 1월 21일
It's more complicated than the answer above but also very useful, thank you !

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 1월 21일

댓글:

2020년 1월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by