How can I plot a 3D matrix as a cube with different transparent colors for the values?

11 visualizzazioni (ultimi 30 giorni)
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!
  5 Commenti
Korosh Agha Mohammad Ghasemi
Steps:
  1. Use patch to create colored and semi-transparent cubes for each element in the 3D matrix.
  2. Assign colors based on the values in the matrix.
  3. Use transparency to visualize overlapping cubes.
% Example 3D matrix
matrix = randi([0, 3], 5, 5, 5); % Replace with your matrix
% Define colors for each value
colors = [
1, 1, 1; % White for 0
1, 0, 0; % Red for 1
0, 1, 0; % Green for 2
0, 0, 1 % Blue for 3
];
% Create figure
figure;
hold on;
axis equal;
xlabel('X'); ylabel('Y'); zlabel('Z');
view(3);
% Iterate over matrix
[nx, ny, nz] = size(matrix);
for x = 1:nx
for y = 1:ny
for z = 1:nz
value = matrix(x, y, z);
if value >= 0 && value <= 3
% Draw cube with specified color
drawCube([x-0.5, y-0.5, z-0.5], colors(value+1, :), 0.5);
end
end
end
end
hold off;
% Function to draw a single cube
function drawCube(center, color, alpha)
% Define cube vertices
vertices = [
0 0 0; 1 0 0; 1 1 0; 0 1 0; % Bottom face
0 0 1; 1 0 1; 1 1 1; 0 1 1 % Top face
];
vertices = vertices - 0.5; % Center around (0,0,0)
vertices = vertices + center; % Shift to desired center
% Define cube faces
faces = [
1 2 6 5; % Bottom
2 3 7 6; % Right
3 4 8 7; % Top
4 1 5 8; % Left
1 2 3 4; % Front
5 6 7 8 % Back
];
% Draw cube
patch('Vertices', vertices, 'Faces', faces, ...
'FaceColor', color, 'FaceAlpha', alpha, ...
'EdgeColor', 'black');
end
Explanation:
  1. Matrix values to colors: Each value (0, 1, 2, 3) is mapped to a specific RGB color.
  2. Transparent cubes: The FaceAlpha property in the patch function sets transparency.
  3. Cubic layout: The cubes are centered at the (x, y, z) indices of the matrix.
The script creates a 3D plot with a cube for each matrix element, colored based on its value, and transparent enough to see overlapping layers.

Accedi per commentare.

Risposta accettata

darova
darova il 24 Lug 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 Commenti
darova
darova il 24 Lug 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
img.png
BananaBandana
BananaBandana il 24 Lug 2019
Thanks!
it worked! But I realized that it takes a lot of time to process for a bigger matrix and therefore is probably is not useable for my case :( I will probably have to think of a different way to visualize it... but thats a different question

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by