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 CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by