Top N values for multidimensional matrix with different N at each grid cell

조회 수: 3 (최근 30일)
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
Ahmet Cecen
Ahmet Cecen 2016년 11월 24일
You need to explain this with a simple example.
Vesp
Vesp 2016년 11월 24일
Here is a more detailed example of what I would like to do. Matrix A= [0 1 5 10 12 12 3 4 8 9 4 6 8 9 0] Matrix A is the N values I would like to extract from the same grid cell in Matrix B. Where Matrix B is a 3x5x50 multidimensional matrix
I would like to take value of Matrix A let's say value at 1x4 = 10. After calling this value I will go to Matrix B and go to the associated cell there 1x4x50 and first sort the data from greater to least and then extract the top 10 greater values. These 10 values will be averaged before being stored into another matrix (Matrix C) at location 1x4. Since this will be done at each grid cell I should have Matrix C as a 3x5 with averages of the top N values from Matrix B (where N values are unique to each grid cell defined by matrix A).
I hope this is clear. I apologize for any confusion.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 11월 26일
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
Vesp
Vesp 2016년 11월 26일
This is great! There is an error at C0 = ... |
_Error using ./
Array dimensions must match for binary array op._
This is giving me the sum at that grid cell. Do you mind explaining what you were trying to do with the ./reshape combination? Thank you for your time.
Andrei Bobrov
Andrei Bobrov 2016년 11월 26일
편집: Andrei Bobrov 2016년 11월 26일
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,[]));

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2016년 11월 24일
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
Vesp
Vesp 2016년 11월 26일
편집: Vesp 2016년 11월 26일
Thank you for your response KSSV. This has helped me get a step closer to what I want to achieve and show me where I am having trouble with the coding. I have tried the code you have provided, but for a 3x4x5 matrix.
A = rand(3,4,5); % random data
% initilaize the required
B= rand(3,4,5); %matrix with the unique top values to extract for that specific grid cell
C = zeros(5,5);
Cavg = zeros(5,1);
for i = 1:5
Ai = sort(A(:,:,i),'descend');
C(:,1) = Ai(1:5);
Cavg(i) = mean(B(:,i));
end
In the for loop Ai I need to have it go through each grid cell as shown: Ai=sort(A(1,1,i,'descend') then Ai=sort(A(1,2,i,'descend') etc until it goes through each grid cell in the entire matrix.
I have also defined a Matrix B which is the matrix for which I want to use to define the number of N values I want to extract for averaging at each grid cell (this value is unique for each grid cell)
Thank you again for your patience and help! I look forward to learning a lot more :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by