开发者

MATLAB see if something is included in matrix

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!


In one line:

cb(xb==1)

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜