Finding an index from a 3D matrix

16 ビュー (過去 30 日間)
Alonso Figueroa
Alonso Figueroa 2023 年 4 月 20 日
編集済み: Dyuman Joshi 2023 年 4 月 20 日
Hello,
I am writing to ask for help in finding the index of a point in a 3D array. I have a 3D matrix of dimentions [M,N,O] and I have reshaped it onto a 1xQ array (Q = M*N*O)
I'm trying to find the index in array Q of a point selected three dimensionally in the MNO matrix. This could easily be performed in 2D ith the 'sub2ind()' function, but I havent been able to do this in 3D.
I would apreciate any help.
Best,
Alonso

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 4 月 20 日
編集済み: Dyuman Joshi 2023 年 4 月 20 日
sub2ind works for 3D arrays as well
%Initial array, with random values for example
in = reshape(1:105,3,5,7);
%Array reshaped into a row vector
out = reshape(in,1,[]);
%Random point in the 3D array
ix = randi(3)
ix = 2
iy = randi(5)
iy = 1
iz = randi(7)
iz = 6
linearindex = sub2ind(size(in), ix, iy, iz)
linearindex = 77
%Verifying the answer as our data is simply 1:numel(in)
in(ix, iy, iz)
ans = 77

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by