How can I plot a 3D matrix as a cube with different transparent colors for the values?
18 views (last 30 days)
Show older comments
Hello,
I have a 3D-Matrix consisting only of values 0,1,2 and 3. Is it possible to plot the matrix so it looks like a big, transparent rubic´s cube, where every value gets its own colour? Like imagesc(...) but only in 3D.
Thanks a lot!
4 Comments
Accepted Answer
darova
on 24 Jul 2019
Since you have matrix of 0,1,2,3 and don't have coordinates you can create them
Assume A - is you matrix (N x N x N)
% (:) - colon operator (make vector from matrix of length N*N*N)
[X,Y,Z] = meshgrid(1:N);
scatter3(X(:),Y(:),Z(:),5,A(:))
3 Comments
darova
on 24 Jul 2019
Create one cube using patch() (or fill3()) then copy it using for loop
clear,clc,cla
x1 = [1 1 1 1]'/2; % right wall
y1 = [1 1 -1 -1]'/2;
z1 = [1 -1 -1 1]'/2;
x2 = [1 1 -1 -1]'/2; % top wall
y2 = [-1 1 1 -1]'/2;
z2 = [1 1 1 1]'/2;
x3 = x2; % bottom wall
y3 = y2;
z3 = -z2;
X = [x1 x2 x3];
Y = [y1 y2 y3];
Z = [z1 z2 z3];
patch(X,Y,Z,'b')
alpha(0.5)
axis equal
I succeded
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots 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!