2

I have a matrix

a = [ 1 'cancer'
      2 'cancer'
      3 'cancer'
      4 'noncancer'
      5 'noncancer' ]

I have another matrix with values

b = [ 4
      5
      2 ]

Now I have to compare the b matrix values with values of a and the output should be

output = [ 4  'noncancer'
           5  'noncancer'
           2  'cancer']

How can I do this in matlab ?

1
  • 1
    a is a cell array and not a matrix. Commented Mar 5, 2013 at 10:08

1 Answer 1

5

You can use ismember:

a = { 1 'cancer'
      2 'cancer'
      3 'cancer'
      4 'noncancer'
      5 'noncancer' };

  b = [ 4
      5
      2 ];

 a(ismember([a{:,1}], b),:)

This results in

ans = 

    [2]    'cancer'   
    [4]    'noncancer'
    [5]    'noncancer'

To display the results in the order specified by b use (as requested in a follow-up question: In the same order, finding an element in an array by comparing it with another array)

[logicIDX, numIDX]  = ismember(b, [a{:,1}]);
a(numIDX, :)

This results in:

ans = 

   [4]    'noncancer'
   [5]    'noncancer'
   [2]    'cancer' 
Sign up to request clarification or add additional context in comments.

Comments

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.