Find subscripts in a 3D matrix

9 ビュー (過去 30 日間)
Fernando Montero
Fernando Montero 2021 年 1 月 28 日
コメント済み: Fernando Montero 2021 年 1 月 28 日
I have a 3D matrix, called A, and I want to find the subscripts for all places where elements of vector B appear. The elements of B apper more than one time in the matrix A.
I tried it using function subs to obtain directly the susbsripts, but I obtained a result where many page numbers are wrong (it shows a higher number (5) than the existing pages (3)). Bellow the code:
A(:,:,1)=[1 2 ; 3 3 ; 5 7 ; 4 2];
A(:,:,2)=[9 1 ; 4 4 ; 9 2 ; 4 6];
A(:,:,3)=[2 4 ; 7 4 ; 3 1 ; 6 4];
B = [9 4 1];
[row,col,page] = ind2sub(size(A),find(A(:)==B))
Also I tried using function find to obtain the linear indexes to later convert them to subscripts; however, I only obtain the first place were the element is placed on the matrix:
n = arrayfun(@(x) find(A==x,1),B);
[row, col, page] = ind2sub(size(A), n);

採用された回答

Walter Roberson
Walter Roberson 2021 年 1 月 28 日
A(:,:,1)=[1 2 ; 3 3 ; 5 7 ; 4 2];
A(:,:,2)=[9 1 ; 4 4 ; 9 2 ; 4 6];
A(:,:,3)=[2 4 ; 7 4 ; 3 1 ; 6 4];
B = [9 4 1];
[row,col,page] = ind2sub(size(A),find(ismember(A,B)))
row = 12×1
1 4 1 2 3 4 1 2 1 2
col = 12×1
1 1 1 1 1 1 2 2 2 2
page = 12×1
1 1 2 2 2 2 2 2 3 3
  4 件のコメント
Walter Roberson
Walter Roberson 2021 年 1 月 28 日
find(ismember(A,B)) would be linear
sub2ind(size(A), row,col,page)
if you need to reconstruct linear
Fernando Montero
Fernando Montero 2021 年 1 月 28 日
Thanks again!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by