フィルターのクリア

Mapping locations of similar rows in two matrices

1 回表示 (過去 30 日間)
Patrick Mboma
Patrick Mboma 2014 年 11 月 1 日
コメント済み: Patrick Mboma 2014 年 11 月 1 日
Dear all,
I have two matrices A and B of respective sizes [na,m] and [nb,m], with na>=nb. Both matrices have the same number of columns. All the rows in B are unique while the rows in A can be duplicated, but are all members of B. I would like to create a mapping between A and B and one very inefficient way to do that is as follows:
out=nan(na,1);
for ii=1:na
% for each row of A, I search its location in B
out(ii)=find(all(bsxfun(@minus,A(ii,:),B),2)==0);
end
This works well but becomes prohibitively expensive for large na and nb. And so my question is whether there is a way of getting rid of the for loop and/or creating this mapping differently.
Thanks,
P.

採用された回答

Guillaume
Guillaume 2014 年 11 月 1 日
This is exactly what ismember is for. The 2nd output argument will give you the locations:
[~, out] = ismember(A, B, 'rows');
  4 件のコメント
Guillaume
Guillaume 2014 年 11 月 1 日
For that specialised function to be any faster than ismember you would have to write it as a mex file.
And if ismember(..., 'rows') is implemented efficiently, it's possible the only saving you could get would be to skip the parsing of the 'rows' argument
Patrick Mboma
Patrick Mboma 2014 年 11 月 1 日
A million thanks Guillaume

サインインしてコメントする。

その他の回答 (1 件)

the cyclist
the cyclist 2014 年 11 月 1 日
Use the ismember function, with the 'rows' option.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by