MATLAB Answers

How to make statements intersect

34 views (last 30 days)
Ani Asoyan
Ani Asoyan on 21 Jan 2020 at 12:36
Commented: Ani Asoyan on 21 Jan 2020 at 13:01
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

  0 Comments

Sign in to comment.

Accepted Answer

Alex Mcaulley
Alex Mcaulley on 21 Jan 2020 at 12:44
a=[0 1 2];
b=[1 1 3];
if all(a==b)
fprintf ('yes')
else
fprintf ('no')
end

  1 Comment

Ani Asoyan
Ani Asoyan on 21 Jan 2020 at 12:58
Thank you I this is all I wanted !!!

Sign in to comment.

More Answers (1)

Bhaskar R
Bhaskar R on 21 Jan 2020 at 12:50
Edited: Bhaskar R on 21 Jan 2020 at 12:52
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 Comment

Ani Asoyan
Ani Asoyan on 21 Jan 2020 at 13:01
It's more complicated than the answer above but also very useful, thank you !

Sign in to comment.

Sign in to answer this question.