Minimum value in sub Matrix
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Zach Hudson
am 26 Mai 2015
Bearbeitet: Stephen23
am 26 Mai 2015
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.
0 Kommentare
Akzeptierte Antwort
Stephen23
am 26 Mai 2015
Bearbeitet: Stephen23
am 26 Mai 2015
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 Kommentare
Stephen23
am 26 Mai 2015
Bearbeitet: Stephen23
am 26 Mai 2015
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Deep Learning Toolbox finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!