ismember for string matrix

I have two string matrices;
A=['c1' 'c ' 'b ' 'd9']'; %UNIQUELIST
B=['d9' 'c1']'; %ORIGINALLIST
I would like do find member of B in A, using:
[LIA,LOCB]=ismember(A,B);
and it returns
LOCB =
3
4
3
0
0
0
1
2
But I actually would like it to return matching row index like this:
LOCB =
2
0
0
1
Thanks for your help
Sam

回答(2 个)

You need to make A and B cell arrays so that each string piece is a separate element (rather than a 1xn string. The fix is simple use {} instead of []
A={'c1' 'c ' 'b ' 'd9'}';
B={'d9' 'c1'}'
[LIA,LOCB]=ismember(A,B)
You've got string arrays here; use the 'rows' optional argument to treat them as such instead of as individual characters...
>> [~,loc]=ismember(A,B,'rows')
loc =
2
0
0
1
>>

3 个评论

i guessed if i use
[~,loc]=ismember(cellstr(A),cellstr(B))
then it would safely do the same job?
Did you try it and see???
Probably, but converting to cells is heavier weight if you don't need them for other things.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Data Type Conversion 的更多信息

标签

提问:

Sam
2014-11-26

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by