Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 通过与另一个数组进行比较来查找数组中的元素_Matlab_Matrix - Fatal编程技术网

Matlab 通过与另一个数组进行比较来查找数组中的元素

Matlab 通过与另一个数组进行比较来查找数组中的元素,matlab,matrix,Matlab,Matrix,我有一个矩阵 a = [ 1 'cancer' 2 'cancer' 3 'cancer' 4 'noncancer' 5 'noncancer' ] 我有另一个矩阵和值 b = [ 4 5 2 ] 现在我要比较b矩阵的值和a的值,结果应该是 output = [ 4 'noncancer' 5 'noncancer' 2 'cancer'] 如何在matlab中执行此

我有一个矩阵

a = [ 1 'cancer'
      2 'cancer'
      3 'cancer'
      4 'noncancer'
      5 'noncancer' ]
我有另一个矩阵和值

b = [ 4
      5
      2 ]
现在我要比较b矩阵的值和a的值,结果应该是

output = [ 4  'noncancer'
           5  'noncancer'
           2  'cancer']
如何在matlab中执行此操作?

您可以使用:

这导致

ans = 

    [2]    'cancer'   
    [4]    'noncancer'
    [5]    'noncancer'
要按
b
指定的顺序显示结果,请使用(根据后续问题中的要求:)

这导致:

ans = 

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

a
是一个单元数组而不是矩阵。
[logicIDX, numIDX]  = ismember(b, [a{:,1}]);
a(numIDX, :)
ans = 

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