Take the 2-minute tour ×

I am having issues manipulating a matrix. Any help would be much appreciated! Say I have a matrix:

xb =

          1.00          2.00          3.00          6.00          5.00          9.00

and another matrix:

cb =

       3000.00       4000.00       4000.00             0             0             0

Is there a way to code something that would check to see if a 1 is within the xb matrix, and display the corresponding cell in cb (the same column that one is)?

so for example, since 6 is in the xb matrix above, the program would display 0. Thanks!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

In one line:


xb==1 creates a logical array of the same size as xb, which is 1 wherever xb is 1, and 0 elsewhere. Since this logical array is of the same size as cb, you can use it for indexing. Indexing an array with a logical array returns all the values of the array at the places where the logical array is 1 (think of it as a mask).

share|improve this answer
you might want to link to these questions regarding floating point comparison: stackoverflow.com/questions/2202641/…, stackoverflow.com/questions/686439/… –  Amro Oct 21 '10 at 20:03
cb(abs(xb-1)<eps) should take care of floating point errors. –  Doresoom Oct 21 '10 at 20:20

Your Answer


