Sort and rearranged a 1D array to 3D matrix

2 views (last 30 days)
K3iTH
K3iTH on 29 Mar 2023
Commented: K3iTH on 30 Mar 2023
Hello all,
I have matrix with the size of 60x150x150. I have reshaped it into a 1D array and sorted it so the value is in descending order.
What I want to do next is rearrange this 1D array back to the the 3D matrix with the size of 60x150x150. The cell is filled in the X direction (150) with Z=1 and starting from 151, it will begin from z=2. After it filling up 22500 cells (XxZ = 150x150), then only it will start from Y=2.
The code below creates TEMP matrix and sorted the 1D named sortedTEMP. May I know how can I do that? I am not sure how to start with putting the cell back into 3D. I have tried reshape function and it is not working.
Thanks for your help!!
xx = linspace(-15,15,150);
zz = linspace(-15,15,150);
yy = linspace(0,1,60);
[X,Y,Z] = meshgrid(xx,yy,zz);
R = sqrt(X.^2 + Z.^2);
TEMP = 1.0+(1.0-tanh((R-1.0)/0.01))*0.5/10.0;
TEMP_norm = (TEMP -1)/0.1;
xslice = []; % location of y-z planes
yslice = 0.5; % location of x-z plane
zslice = []; % location of x-y planes
slice(X,Y,Z,TEMP_new,xslice,yslice,zslice);
xlabel('x')
ylabel('y')
zlabel('z')
oneDTEMP = reshape(TEMP,[],1); % reshape into 1D array
sortedTEMP = sort(oneDTEMP,'descend'); % rearrange value from high to low
TEMP_new = zeros(size(TEMP));
  1 Comment
K3iTH
K3iTH on 30 Mar 2023
Hello all,
I have managed to solve this by myself.
Thanks for your help!

Sign in to comment.

Answers (1)

Antoni Garcia-Herreros
Antoni Garcia-Herreros on 29 Mar 2023
Hello,
You could add these lines to the end of your code
[sx,sy,sz]=size(TEMP);
A=reshape(sortedTEMP,[sx*sz,sy])';
for i=1:sx
Z(i,:,:)=A(:,(i-1)*sz+1:(i-1)*sz+sz);
end

Categories

Find more on Data Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!