Top N values for multidimensional matrix with different N at each grid cell
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Vesp
el 23 de Nov. de 2016
Editada: Andrei Bobrov
el 26 de Nov. de 2016
Hello all,
I have been looking for an efficient way to execute the following, but haven't come across anything in the forum. I would like to find the top N values in a multidimensional array at each grid cell of Matrix B (144x90x1877). I have found the following approach in the forum, but it gives the top N values for the entire multidimensional matrix.
n= 2; % any number specified
[a,ix] = sort(A(:),'descend');
a = a(1:n);
ix = ix(1:n);
I would like to have the top N values for each grid cell be defined by another matrix(Matrix A, 2D matrix). For example, I would like to start with cell 1x1 matrix A which specifies 5 top N values so I go to Matrix B 1x1x8766 and sort the values in descending order and extract the top 5 values to be average and stored in another matrix. Continue with each cell.
I would appreciate any guidance on how to proceed with this. Thank you
2 comentarios
Respuesta aceptada
Andrei Bobrov
el 26 de Nov. de 2016
Let A and B:
A= [0 1 5 10 12
12 3 4 8 9
4 6 8 9 0]
B = randi([3 8],3,5,15);
solution:
t = A > 0;
[m,n,k] = size(B);
C = zeros(m,n);
C0 = cumsum(sort(B,3,'descend'),3)./reshape(1:k,1,1,[]);
[ii,jj] = ndgrid(1:m,1:n);
C(t) = C0(sub2ind([m,n,k],ii(t),jj(t),A(t)));
2 comentarios
Andrei Bobrov
el 26 de Nov. de 2016
Editada: Andrei Bobrov
el 26 de Nov. de 2016
Your version before the Matlab R2016b.
For old version of Matlab:
C0 = bsxfun(@rdivide,cumsum(sort(B,3,'descend'),3),reshape(1:k,1,1,[]));
Más respuestas (1)
KSSV
el 24 de Nov. de 2016
A = rand(144,90,1877) ; % random data
% initilaize the required
B = zeros(1877,5) ;
Bavg = zeros(1877,1) ;
for i = 1:1877
Ai = sort(A(:,:,i),'descend') ;
B(i,:) = Ai(1:5) ;
Bavg(i) = mean(B(i,:)) ;
end
1 comentario
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!