Return subscripts of common rows for multi-dimensional matrix?

I have a 8x2 matrix, A, and a 133x2x5 matrix, B. I want to return the the layer in B in which a row in A matches a row in B. How can I do that? I tried using intersect and ismember but have not had any luck thus far. Having a hard time with the matrix being multi-dimensional.

4 件のコメント

Guillaume
Guillaume 2018 年 7 月 13 日
Is it the location, row, col and layer of any B element that is also in A?
Or the row and layer of the 1x2x1 rows of B that are also rows of A?
Andrew Poissant
Andrew Poissant 2018 年 7 月 13 日
Sorry, I was confusing in my original post. I want the layer in B in which any complete row in A matches any complete row in B.
Guillaume
Guillaume 2018 年 7 月 13 日
And you don't care about which is the row in B that match a A row in that layer?
Andrew Poissant
Andrew Poissant 2018 年 7 月 13 日
I do not care about that. It can be any row in B in any layer, as long as it matches to a row in A.

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

 採用された回答

Guillaume
Guillaume 2018 年 7 月 13 日
編集済み: Guillaume 2018 年 7 月 13 日

0 投票

[row, layer] = ind2sub([size(B, 1), size(B, 3)], find(ismember(reshape(permute(B, [1 3 2]), [], size(B, 2)), A, 'rows')))
If you want just the layers in which any row matches any row of A:
layer = unique(layer)
edit: By the way the logic of this is to reshape B into a two column matrix by vertically concatenating the layers. Then use the traditional ismember(..., 'rows') and finally convert the matched rows back into (row, layer) coordinate.
Another way, avoiding the sub2ind would be:
layer = unique(ceil(find(ismember(reshape(permute(B, [1 3 2]), [], size(B, 2)), A, 'rows')) / size(B, 1)))

その他の回答 (1 件)

dpb
dpb 2018 年 7 月 13 日

0 投票

ix=mod(find(all(ismember(A,B),2)),size(A,3));

4 件のコメント

Guillaume
Guillaume 2018 年 7 月 13 日
I'm not sure I understand the logic of this, but isn't this guaranteed to return some 0 ix because of the mod?
dpb
dpb 2018 年 7 月 13 日
編集済み: dpb 2018 年 7 月 13 日
Yes, that indicates last row of given plane...the logic is simply to find the rows in the 2D vector that match (fake the 'rows' option for 3D array A) and then the modulo returns the plane given the size of the third dimension.
Andrew Poissant
Andrew Poissant 2018 年 7 月 13 日
Thank you for the answer but I went with dpb's answer because yours was returning 0s.
dpb
dpb 2018 年 7 月 13 日
It is dpb's and it's supposed to be zero...the "fixup" is
ix(ix==0)=size(A,3);
I posted it as much as a lark as anything... :) G's is a much more legible and therefore maintainable approach.

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2018 年 7 月 13 日

コメント済み:

dpb
2018 年 7 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by