Minimum value in sub Matrix

3 ビュー (過去 30 日間)
Zach  Hudson
Zach Hudson 2015 年 5 月 26 日
編集済み: Stephen23 2015 年 5 月 26 日
I have a 4 dimensional matrix that represents an array of arrays and I need to find the minimum value and its index in each of the sub arrays. IE I need to call min(example(:)) on example(:,:,1,1), example(:,:,1,2)... example(:,:,1,m), example(:,:,2,1)... example(:,:,2,m)... example(:,:,n,m). Any help with this would be much appreciated.

採用された回答

Stephen23
Stephen23 2015 年 5 月 26 日
編集済み: Stephen23 2015 年 5 月 26 日
You can just use min with its optional dimension argument:
X = [4D array];
Y = min(min(X,[],1),[],2);
Here is a simple example, with a 3D array:
>> X = reshape(1:24, [2,3,4])
X(:,:,1) =
1 3 5
2 4 6
X(:,:,2) =
7 9 11
8 10 12
X(:,:,3) =
13 15 17
14 16 18
X(:,:,4) =
19 21 23
20 22 24
>> Y = min(min(X,[],1),[],2)
Y(:,:,1) =
1
Y(:,:,2) =
7
Y(:,:,3) =
13
Y(:,:,4) =
19
You can see then it has calculated the minimum of the first two dimensions only, leaving the third dimension remaining. This works for 4D arrays too. If you do not want the leading scalar-dimensions to remain, simply use squeeze to remove them:
>> squeeze(Y)
ans =
1
7
13
19
  2 件のコメント
Zach  Hudson
Zach Hudson 2015 年 5 月 26 日
This works to get the minimum value but I would really like the index as well.
Stephen23
Stephen23 2015 年 5 月 26 日
編集済み: Stephen23 2015 年 5 月 26 日
This is possible, but more complicated. Here is an example for a 3D array:
>> X = reshape(randperm(24), [2,3,4])
X(:,:,1) =
7 21 12
20 6 24
X(:,:,2) =
14 3 8
22 10 15
X(:,:,3) =
19 5 16
11 4 1
X(:,:,4) =
13 23 18
2 17 9
>> [Y,idy] = min(X,[],1);
>> [Z,idz] = min(Y,[],2);
>> N = size(X,3);
>> idy(sub2ind(size(idy),ones(N,1),squeeze(idz),(1:N)')) % row indices
ans =
2
1
2
2
>> squeeze(idz) % column indices
ans =
2
2
3
1
>> squeeze(Z) % the minimum values
ans =
6
3
1
2
This can be extended to work with 4D arrays... with some effort. It might be easier to perform this operation independently for each slice of the fourth dimension.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDeep Learning Toolbox についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by