how to plot using the slice function the last row and column of a 3 matrix?

조회 수: 10 (최근 30일)
Rabih Sokhen
Rabih Sokhen 2023년 3월 31일
댓글: Rabih Sokhen 2023년 4월 6일
Hello, guys
I am trying to do the slicing of a 3d matrix, however I notice that the slice function during the plot do not show us the last column and row as we can see in the following code:
Is there a way to plot all the value of a 3 matrix during the slicing?
Thank you
Best regards
code:
clear all
clc
a=randi(5,3,3,3);
slice(a,1,[ ],[ ])

답변 (1개)

Adam Danz
Adam Danz 2023년 3월 31일
편집: Adam Danz 2023년 3월 31일
The syntax slice(V,xslice,yslice,zslice) plots surface using the vertex data in V at slices specifed by the 2nd, 3rd, and 4th arguments. Vertices define the edges of each face. Along a single dimension, a single face has two vertices so along a single dimension you will have a number of face equal to one minus the number of vertices.
Take this reproducible example that plots the faces of a 4x4x4 array along the first slice of the x-axis. It also adds a colorbar so we can see how the data maps to the colors.
rng(6) % for reproducibility
a=randi(5,4,4,4);
slice(a,1,[ ],[ ])
colormap(jet(10))
colorbar
The colors in the bottom row of the surface are defined by
a(:,1,1)
ans = 4×1
5 2 5 1
where 5 is red, 2 is blue, 5 is red, and the next face, if there was one, would be dark blue.
The colors in the middle row of the surface are defined by
a(:,1,2)
ans = 4×1
5 3 5 5
where 5 is red, 3 is mint green, 5 is red, and the next face, if there was one, would be red.
The colors at the top row of the surface are defined by
a(:,1,3)
ans = 4×1
2 4 3 4
where 2 is blue, 4 is yello, 3 is mint green, and the next face, if there was one, would be yellow.
In summary, the plot is produced again below and the vertices of the bottom row are numbered. If you interpolate the FaceColor, you can see the last edge of the vertices effet the colors.
figure
tiledlayout(1,2)
nexttile()
slice(a,1,[ ],[ ])
colormap(jet(10))
colorbar
text([1 1 1 1]-.2, [1 2 3 4], [1 1 1 1], string([1,2,3,4]),'FontSize', 14)
axis square
title('Flat')
nexttile()
h = slice(a,1,[ ],[ ]);
h.FaceColor = 'interp';
colormap(jet(10))
axis square
title('Interpolated')
  댓글 수: 3
Adam Danz
Adam Danz 2023년 4월 4일
You'll either need to pad your array or you can slice it yourself and user imagesc or heatmap
a=randi(5,3,3,3);
m = squeeze(a(1,:,:));
heatmap(m)
colormap(jet)
Rabih Sokhen
Rabih Sokhen 2023년 4월 6일
hi Adam
"I tried to write the following code, but when I attempt to slice in two directions simultaneously - along both the x and y axes - the resulting values for the x and y axes are not between -1 and 1. Do you have any ideas on how to fix this?"
thank you in advance
code:
clear all
clc
a= randi(2,3,4,5)
x=linspace(-1,1,size(a,1));
y=linspace(-1,1,size(a,2));
z=linspace(-1,1,size(a,3));
slice_3D(x,y,z,a,[],[],z)
colorbar
caxis([1 2])
colormap(jet(2))
function slice_3D(varargin)
if numel(varargin) == 4
[a, x_slice, y_slice, z_slice] = deal(varargin{1:4});
a(end+1,:,:)=nan;
a(:,end+1,:)=nan;
a(:,:,end+1)=nan;
x=linspace(1-0.5,size(a,1)-0.5,size(a,1));
y=linspace(1-0.5,size(a,2)-0.5,size(a,2));
z=linspace(1-0.5,size(a,3)-0.5,size(a,3));
hh=slice(x,y,z,permute(a,[2,1,3]),x_slice,y_slice,z_slice);
set(hh,'EdgeColor','none','FaceAlpha',1);
xlabel('x');
ylabel('y');
zlabel('z');
elseif numel(varargin) == 7
[x ,y ,z, a, x_slice, y_slice, z_slice] = deal(varargin{1:7});
dx=x(2)-x(1);
dy=y(2)-y(1);
dz=z(2)-z(1);
a(end+1,:,:)=nan;
a(:,end+1,:)=nan;
a(:,:,end+1)=nan;
if(isempty(z_slice)~=1 && isempty(y_slice)~=0 && isempty(x_slice)~=0)
x=linspace(x(1),x(end),size(a,1));
y=linspace(y(1),y(end),size(a,2));
z=linspace(z(1),z(end)+dz,size(a,3));
end
if(isempty(y_slice)~=1 && isempty(x_slice)~=0 && isempty(z_slice)~=0)
x=linspace(x(1),x(end),size(a,1));
y=linspace(y(1),y(end)+dy,size(a,2));
z=linspace(z(1),z(end),size(a,3));
end
if(isempty(x_slice)~=1 && isempty(y_slice)~=0 && isempty(z_slice)~=0)
x=linspace(x(1),x(end)+dx,size(a,1));
y=linspace(y(1),y(end),size(a,2));
z=linspace(z(1),z(end),size(a,3));
end
hh=slice(x,y,z,permute(a,[2,1,3]),x_slice,y_slice,z_slice);
set(hh,'EdgeColor','none','FaceAlpha',1);
xlabel('x');
ylabel('y');
zlabel('z');
else
error('IMG: incorrect number of arguments')
end
view(-25,20)
end

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

카테고리

Help CenterFile Exchange에서 Eigenvalues에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by