Need help writing an if statement involving vectors. Total mental block :/

I have a vector:
A=[14 13 12 11 10]
And I'm given another vector B that's the same length of A, and contains any numbers.
I need to write an if statement determining if this vector B contains the same numbers as A (even if the numbers are arranged differently than in A)
NOTE: INCORRECT SYNTAX, but here's the idea
if B==A
disp('B is equal to A')
else
end

댓글 수: 3

Since you Accepted my Answer by accident, I deleted it. (I get the points anyway, but you can now Accept Charlie’s Answer so he will as well.)
Ok thanks. I appreciate your help anyways.
My pleasure. It’s not the first time I misinterpreted a Question!

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

 채택된 답변

Charlie Elverson
Charlie Elverson 2015년 5월 5일
편집: Charlie Elverson 2015년 5월 5일

0 개 추천

I think you could use the ismember() function to do what you need. It'll return a vector of ones and zeros, so you would just have to check the returned vector for any zeros.
EDIT: if you use all(ismember(A, B)), I think that'll do the trick.

댓글 수: 3

I accidentally Accepted the first response I got as the answer, you were right. It didn't work. I will try the all(ismember(A,B)) and I'll get back to you.
Works Perfectly! Thank you! I've got one more problem for you, if you're up for it :) This one is probably a bit more tricky.
A new problem is best posed as a new question.

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by