How to rank 3d matrix across 2 dimensions

7 views (last 30 days)
Hi Everyone,
I have 5 variables of gridded data that are identical in size (361x181 matrices). I have run these through different scenerios for my anaylsis and now am trying to compare the changes. What I would like to do is create a 3-d matrix of all the variables sorted by highest to lowest (361x181x5) but retain the "name" of the vairable so I can say this variable had the most change at this location.
From what I have been able to find online the best way seems to be to create a cell of off the variables and then sort.
The issue i am having is sorting the cell along the correct dimesions with the associated names. See the code I've tried so far below, the variables are attached
iVars = zeros(361,181,5);
iVars(:,:,1) = var1;
iVars(:,:,2) = var2;
iVars(:,:,3) = var3;
iVars(:,:,4) = var4;
iVars(:,:,5) = var5;
iNames = {'Var 1','Var 2','Var 3','Var 4','Var 5'};
[~,VarSORT]=sort(iVars,3,'descend');
iNames(VarSORT);
Any help or suggestions is greatly appreciated!
  4 Comments
James Spalding
James Spalding on 7 Nov 2022
For clarity, my end goal is to have either a cell-like strucutre that is 361x181, and in each cell value it will have a list/table ranking the initial values from the 5 variables at every index (decending order).
So for example "finalRank" (10,10) will have a list of the associated names in decending order of the values from var1(10,10) var2(10,10) var3(10,10) var4(10,10) and var5(10,10).
If possible it would be nice to have the associated values in ranked order too but tha tis less critical to me
Thanks
James
James Spalding
James Spalding on 7 Nov 2022
Hi all,
So i have found a "forced" solution for this problem that takes a while but does work (see code below)
If anyone can come up with a better solution that doesn't run the risk of my computer suddenly bursting into flames let me know I'd appreciate it!
James
%rank vars in single variable
iVars = zeros(361,181,5);
iVars(:,:,1) = var1;
iVars(:,:,2) = var2;
iVars(:,:,3) = var3;
iVars(:,:,4) = var4;
iVars(:,:,5) = var5;
VarsRank=cell(361,181);
for j = 1:361
for k = 1:181
val1=table(iVars(j,k,1),"Var1");
val2=table(iVars(j,k,2),"Var2");
val3=table(iVars(j,k,3),"Var3");
val4=table(iVars(j,k,4),"Var4");
val5=table(iVars(j,k,5),"Var5");
irnk=[val1;val2;val3;val4;val5];
RNK=sortrows(irnk,1,'descend');
VarsRank{j,k}=irnk;
fprintf("on iteration\n")
t=j*k;
disp(t)
fprintf('of 65341')
end
end

Sign in to comment.

Answers (2)

Matt J
Matt J on 7 Nov 2022
Is this (simplified) not what you want?
iVars=rand(3,3,5);
iNames = {'Var 1','Var 2','Var 3','Var 4','Var 5'};
[~,VarSORT]=sort(iVars,3,'descend');
iNames=iNames(VarSORT)
iNames = 3×3×5 cell array
iNames(:,:,1) = {'Var 5'} {'Var 4'} {'Var 1'} {'Var 2'} {'Var 1'} {'Var 1'} {'Var 4'} {'Var 3'} {'Var 3'} iNames(:,:,2) = {'Var 2'} {'Var 1'} {'Var 4'} {'Var 5'} {'Var 3'} {'Var 4'} {'Var 2'} {'Var 5'} {'Var 5'} iNames(:,:,3) = {'Var 1'} {'Var 3'} {'Var 5'} {'Var 3'} {'Var 2'} {'Var 2'} {'Var 5'} {'Var 1'} {'Var 1'} iNames(:,:,4) = {'Var 4'} {'Var 5'} {'Var 3'} {'Var 1'} {'Var 5'} {'Var 5'} {'Var 3'} {'Var 4'} {'Var 2'} iNames(:,:,5) = {'Var 3'} {'Var 2'} {'Var 2'} {'Var 4'} {'Var 4'} {'Var 3'} {'Var 1'} {'Var 2'} {'Var 4'}

Bruno Luong
Bruno Luong on 7 Nov 2022
iNames(VarSORT);
won't change iNames, you need to assign the expression to a variable VarsRank
iVars = zeros(361,181,5);
iVars(:,:,1) = var1;
iVars(:,:,2) = var2;
iVars(:,:,3) = var3;
iVars(:,:,4) = var4;
iVars(:,:,5) = var5;
iNames = {'Var 1','Var 2','Var 3','Var 4','Var 5'};
[~,VarSORT]=sort(iVars,3,'descend');
VarsRank = iNames(VarSORT); % Assign the result to a variable

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!