# Sort and rearranged a 1D array to 3D matrix

4 Ansichten (letzte 30 Tage)
K3iTH am 29 Mär. 2023
Kommentiert: K3iTH am 30 Mär. 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.
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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
K3iTH am 30 Mär. 2023
Hello all,
I have managed to solve this by myself.

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Antoni Garcia-Herreros am 29 Mär. 2023
Hello,
[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
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by