通过与另一个数组比较在数组中查找元素 [英] finding an element in an array by comparing it with another array
本文介绍了通过与另一个数组比较在数组中查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个矩阵
a = [ 1 'cancer'
2 'cancer'
3 'cancer'
4 'noncancer'
5 'noncancer' ]
我还有另一个值矩阵
b = [ 4
5
2 ]
现在我必须将b矩阵值与a的值进行比较,并且输出应为
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']
如何在matlab中做到这一点?
How can I do this in matlab ?
推荐答案
You can use 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'
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, :)
结果是:
ans =
[4] 'noncancer'
[5] 'noncancer'
[2] 'cancer'
这篇关于通过与另一个数组比较在数组中查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文