getting the max and its positions from a 4D array

I'm trying to find a way to get the Max value from a 4D array and get the position of each of the max values of the 4D array so that I can trace back and get those positions from another 4D array. It would be great if someone can help me. Thanks in advance!
What I want is to get all the max values of each individual rows and columns and the 3rd dimension and its positions in the 4D.

2 件のコメント

Guillaume
Guillaume 2019 年 9 月 11 日
each of the max
along which dimension(s)? Are you expected a 3D, 2D, 1D, or scalar value in return?
Sanghyun Lee
Sanghyun Lee 2019 年 9 月 11 日
well I guess the max dimensions is 3D but what I want to find is the 4D dimension/positions of each of the max values from which the dimension

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

回答 (2 件)

Star Strider
Star Strider 2019 年 9 月 11 日

2 投票

Try this:
A = rand(3,3,3,3); % Create Array
Amax = max(A(:)); % Maximum Value
Idx = find(A(:) == Amax); % Returns Linear Indices To All ‘Amax’ Values
[r,c,dim3,dim4] = ind2sub(size(A), Idx); % Returns Subscripts In Each Dimension For All ‘Amax’ Values

12 件のコメント

Guillaume
Guillaume 2019 年 9 月 11 日
編集済み: Guillaume 2019 年 9 月 11 日
Since Sanghyun is using R2019a:
[Amax, idx] = max(A, 'all');
[r,c,dim3,dim4] = ind2sub(size(A), Idx);
However, since Sanghyun asks for each of the max, I don't think that's what he/she's after.
Sanghyun Lee
Sanghyun Lee 2019 年 9 月 11 日
yeah neither this or the code above seems to work because it's only getting one max value. What I want is to get all the max values of each individual rows and columns and the 3rd dimension and its positions in the 4D
Star Strider
Star Strider 2019 年 9 月 11 日
True, however not everyone who reads this likely does.
Since there could be several occurrences of the maximum value, I chose to use find to get indices into all of them. From the documentation on the I output of max:
If the largest element occurs more than once, then I [the index returned by max] contains the index to the first occurrence of the value.
(I add this as an extended explanation of my approach for anyone else who reads this.)
Star Strider
Star Strider 2019 年 9 月 11 日
@Sanghyun Lee — I do not understand your Comment. My code will find all the linear indices to every value equal to the maximum value for the entire matrix, returning them in ‘Idx’. It then converts these to the appropriate subscripts, returned in ‘[r,c,dim3,dim4]’.
Sanghyun Lee
Sanghyun Lee 2019 年 9 月 11 日
From what I figured out from using that code, it only finds 1 max value and its position. Therefore it only shows one colour
Sanghyun Lee
Sanghyun Lee 2019 年 9 月 11 日
編集済み: Sanghyun Lee 2019 年 9 月 11 日
Turns out the code I needed was:(but I still need to simplify the nested for loop)
[~,index]=max(totalDistance,[],4);
[rows,cols]=size(index);
finalPixels=zeros(rows,cols,3);
for i=1:rows
for j=1:cols
finalPixels(i,j,1)=imagesList(i,j,1,index(i,j));
finalPixels(i,j,2)=imagesList(i,j,2,index(i,j));
finalPixels(i,j,3)=imagesList(i,j,3,index(i,j));
end
end
Star Strider
Star Strider 2019 年 9 月 11 日
OK. I had no idea what you want to do with respect to your question. If you have several values equal to the maximum value in your array, the find call will detect all of them, and return their indices. Apparently, you want to get the maximum values with respect to the fourth dimension only. That wasn’t initially obvious.
Guillaume
Guillaume 2019 年 9 月 11 日
編集済み: Bruno Luong 2019 年 9 月 11 日
I'm a bit confused by that code. If totalDistance is 4D, then index is going to be 3D. So,
[rows, cols] = size(index);
rows is going to be size(index, 1), and cols is going to be size(index, 2) * size(index, 3). Typically, you'd use
[rows, cols, pages] = size(index);
to get the sizes in each dimensions. I guess the code works but it flattens dimensions 2 and 3 into one dimension.
Bruno Luong
Bruno Luong 2019 年 9 月 11 日
編集済み: Bruno Luong 2019 年 9 月 11 日
Guillaume the only explanation is that the dimension 3 of TotalDistances is a singleton.
Sanghyun Lee
Sanghyun Lee 2019 年 9 月 11 日
編集済み: Sanghyun Lee 2019 年 9 月 11 日
yeah so the 3rd dimension is 1
oh wait I just realised this.
so is this why it wasn't working before?
Guillaume
Guillaume 2019 年 9 月 11 日
Don't knoiw what before refers to.
oh wait I just realised this.
Really? you don't know what the size of each dimension is? Then, yes you're going to struggle writing your code if you don't know what is happening with your dimensions.
Sanghyun Lee
Sanghyun Lee 2019 年 9 月 12 日
well no I knew it in my brain but my mind just realised

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

Andrei Bobrov
Andrei Bobrov 2019 年 9 月 12 日

0 投票

Maybe this:
[m,n,k,f] = size(totalDistance);% here k = 1
[~,ij] = max(totalDistance,[],4);
[I,J,K] = ndgrid(1:m,1:n,1:3);
F = repmat(ij,1,1,3);
finalPixels = imagesList(sub2ind([m,n,3,f],I,J,K,F)); % [m,n,3,f] size of imagesList

カテゴリ

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

製品

リリース

R2019a

タグ

質問済み:

2019 年 9 月 11 日

回答済み:

2019 年 9 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by