Getting dimension indexes based on element index

10 次查看(过去 30 天)
Hi,
I have a 5D matrix, I found the minimum value and minimum index along the whole elements.
How can I know this minimum belongs to which row, which column, which page. I mean for example if the minimum index is 1108.
How can I know at which row, column and pages, this elmentes locates?
Thanks.

采纳的回答

Karim
Karim 2022-8-30
You can use the ind2sub function for this (link --> Convert linear indices to subscripts - MATLAB ind2sub (mathworks.com) )
See below for an example
% random 5D matrix
M = rand(10,10,10,10,10);
% find the minium and its index
[min,idx] = min(M,[],'all')
min = 5.4030e-06
idx = 55455
% convert to subscripts
[I1,I2,I3,I4,I5] = ind2sub(size(M),idx)
I1 = 5
I2 = 6
I3 = 5
I4 = 6
I5 = 6

更多回答(1 个)

Chunru
Chunru 2022-8-30
a = randn(2,3,4,5,6);
[amin, i]=min(a(:))
amin = -2.6982
i = 618
[i1, i2, i3, i4, i5] = ind2sub([2,3,4,5,6], i)
i1 = 2
i2 = 3
i3 = 3
i4 = 1
i5 = 6

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by