0

Say I have

a=[1 2 3 4; 5 6 7 8];

If I then have x=3, y=7, how can I check that (3,7) exists in array a but also ensure that if I check for the pair x=3, y=8 (3,8), then it returns false and NOT true?

EDIT: (3,7) should return true but (3,8) false because 3 and 7 are in the same column, but 3 and 8 are not. Also (7,3) should be false because for (x,y), xcorresponds to element in 1st row and y in 2nd row

EDIT2: I see isPresent = any(ismember(a.', [x y], 'rows')); for the arrray a.

But what if I have this: b=[1 5; 2 6; 3 7; 4 8]. Then how can I ensure that (3,7) is true but (7,3) is false?

5
  • Why would (3,8) return false, because (3,7) was already found, because 3 and 8 are not in the same column, or something else? Commented May 23, 2018 at 22:19
  • because 3 and 8 are not in the same column Commented May 23, 2018 at 22:20
  • Examples with (3,7) and (7,3) will be same for your case? Commented May 23, 2018 at 22:22
  • (3,7) should be true but (7,3) should be false Commented May 23, 2018 at 22:23
  • @user5739619 In the second example, 3 does not exist in the first row, nor does 7 exist in the second row. Please be explicit about the dimensions in which you are searching. Commented May 23, 2018 at 22:47

1 Answer 1

4

The easiest way is to use ismember, but it works on rows instead of columns, so we'll need to transpose the matrix first:

x = 3;
y = 7;
a=[1 2 3 4; 5 6 7 8];

isPresent = any(ismember(a.', [x y], 'rows'));

>> isPresent
isPresent = 1
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that works. But what if I have this: b=[1 5; 2 6; 3 7; 4 8]. Then how can I ensure that (3,7) is true but (7,3) is false?
don't transpose then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.