# How do I plot a 3D Array in MATLAB?

9 次查看（过去 30 天）
Kelvin Anto2021-11-8

I'm trying to plot a 3D Array which was created by the following. I have gone through similar posts, however this question is not answered yet.
My goal is to plot the Amatrix( 3D array) to show its variation with respect to n1, c1 and c2. Please note the n1, c1 and c2 values are stored in n1matrix, c1matrix and c2matrix.
%%% Initializations %%%%
number_of_simulations = 50;
n1vector = linspace(0.55,10,number_of_simulations);
c1vector = linspace(0.55,10,number_of_simulations);
c2vector = linspace(0.55,10,number_of_simulations);
n1matrix = zeros(length(n1vector), length(c1vector), length(c2vector));
c1matrix = zeros(length(n1vector), length(c1vector), length(c2vector));
c2matrix = zeros(length(n1vector), length(c1vector), length(c2vector));
%%% code for creating Amatrix, n1matrix, c1matrix and c2matrix - all 3D matrices of 50X50X50 (rowXcolumnXpage)
for i=1:length(n1)
n1 = n1vector(i);
for j=1:length(c1)
c1 = c1vector(j);
for k=1:length(c2)
c2 = c2vector(k);
Amatrix(i,j,k) = log(n1) + log(c1) + log(c2);
n1matrix(i,j,k) = n1;
c1matrix(i,j,k) = c1;
c2matrix(i,j,k) =c2;
end
end
end

### 采纳的回答

Hi Kelvin,
I understand you wish to plot Amatrix with repect to n1matrix, c1matrix and c2matrix. All are 3d matrices of 50x50x50.
this can be done in the following way -
%%% Initializations %%%%
number_of_simulations = 50;
n1vector = linspace(0.55,10,number_of_simulations);
c1vector = linspace(0.55,10,number_of_simulations);
c2vector = linspace(0.55,10,number_of_simulations);
n1matrix = zeros(length(n1vector), length(c1vector), length(c2vector));
c1matrix = zeros(length(n1vector), length(c1vector), length(c2vector));
c2matrix = zeros(length(n1vector), length(c1vector), length(c2vector));
%%% code for creating Amatrix, n1matrix, c1matrix and c2matrix - all 3D matrices of 50X50X50 (rowXcolumnXpage)
for i=1:length(n1vector)
n1 = n1vector(i);
for j=1:length(c1vector)
c1 = c1vector(j);
for k=1:length(c2vector)
c2 = c2vector(k);
Amatrix(i,j,k) = log(n1) + log(c1) + log(c2);
n1matrix(i,j,k) = n1;
c1matrix(i,j,k) = c1;
c2matrix(i,j,k) =c2;
end
end
end
%%using plot for a particular instance -
figure()
plot(c1matrix(:,:,1),Amatrix(:,:,1))
%% using a plot3() function for a particular instance -
figure()
plot3(c1matrix(:,:,1),c2matrix(:,:,1),Amatrix(:,:,1))
%%Using plot function for 50 iterations -
figure();
for i = 1:50
plot(c2matrix(:,:,i),Amatrix(:,:,i))
hold on;
end
hold off;
figure();
for j = 1:50
plot(c1matrix(:,:,j),Amatrix(:,:,j))
hold on;
end
hold off;
figure();
for k = 1:50
plot(n1matrix(:,:,k),Amatrix(:,:,k))
hold on;
end
hold off
Refer to the MATLAB Documentation link of 'plot',and 'plot3' functions.

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by