Sort and rearranged a 1D array to 3D matrix
조회 수: 2 (최근 30일)
이전 댓글 표시
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개)
Antoni Garcia-Herreros
2023년 3월 29일
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
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!