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

标签 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 中做到这一点?

最佳答案

您可以使用ismember :

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

  b = [ 4
      5
      2 ];

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

这会导致

ans = 

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

要按 b 指定的顺序显示结果,请使用(按照后续问题中的要求: In the same order, finding an element in an array by comparing it with another array )

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

这会导致:

ans = 

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

关于matlab - 通过与另一个数组进行比较来查找数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15220920/

相关文章:

matlab - 将文件夹中名为 1, 2, 3,..., 10, 11, ... 的文件按顺序重命名为 001, 002, 003,..., 010, 011,...

Matlab 音频播放器数组

python - 在python中请求对称矩阵

algorithm - 为什么在 Matlab 中随着 N 的增加,同一个 N * N 矩阵会得到两个不同的逆矩阵?

无法从 'double' 转换为 'double (*)'

matlab - 在 Matlab 中向矩阵添加 header

Matlab imagesc 爬到 OpenCV

unit-testing - 单元测试/与 Simulink/Stateflow 的持续集成

python - 如何在 numpy 中转储 bool 矩阵?

python - 将值是不同长度列表的字典转换为数据框