Change size 3D matrix

Hi, I have a 3D matrix defined as: EP(N,k,r) and I want to plot its values as different surfaces at constant N. So I tried to plot this matrix in this way:
col=hsv(N);
figure
hold on
for f=1:N
surf(vector_1,vector_2,EP(f,:,:),'Color',col(f,:,:));
legendInfo{f} = [num2str(Temp(f)-273.15) ' °C'];
end
legend(legendInfo)
but it doesn't work. It seems I can only plot surfaces at constant r! So now I want to try to plot a new matrix in the form (k,r,N).
How can I change the dimension of matrix EP(N,k,r) (and so its values inside) in this form: EP_1(k,r,N)?

6 Comments

You want to plot slices of a volume?
I want to plot surfaces. Different surfaces at different value of "N". So in the x-axis I have a vector of size k, in y axis a vector of size r, and in the third axis the values of matrix EP (at constant "N").
What about slice or volumeslice?
Francesco Guaresi
Francesco Guaresi on 2 Nov 2019
Edited: Francesco Guaresi on 2 Nov 2019
I think I need “slices”. Actually, I want something like in this picture: different surfaces in a 3D plot
When you say, "it doesn't work", what do you mean, specifically? Do you mean that the code runs, but gives an unexpected result, or that the code does not run, and gives an error? If that latter, please post the complete error message.
Francesco Guaresi
Francesco Guaresi on 2 Nov 2019
Edited: Francesco Guaresi on 2 Nov 2019
The entire code works, except this plot. The error message is:
Error using surf (line 71)
Z must be a matrix, not a scalar or vector.
Error in Isothermal_PFR_EP3_tensor_plot_3D (line 437)
surf(vector_1,vector_2,EP(f,:,:),'Color',col(f,:,:));

Sign in to comment.

Answers (1)

I think what you want is to use
permute(EP(f,:,:),[2 3 1])
as the input, instead of just EP(f,:,:), so that you have a 2-d matrix, rather than a 3-d (in which the first dimension has length 1).

6 Comments

I've obtained this graph, but I need a 3D graph:
figure26.png
Can you upload the variables that you are using to make the plot, in a *.mat file?
Yes! I've written this matlab file (the original one is more long):
clc
clear all
close all
Temp = [600 650 700 750] + 273.15;
xV_vector = [.2 .3 .4 .5 .6];
H_D_ratio = [6 7 8 9 10];
N = max(size(Temp));
k = max(size(xV_vector));
r = max(size(H_D_ratio));
col=hsv(N);
% EP3 matrix is generated by other computations in my original matlab file,
% here below the values obtained:
EP3(:,:,1) = 1.0e+06 * [2.4819 2.7183 2.2334 1.1416 -0.8249;
3.7156 3.5296 2.8405 1.6296 -0.4149;
4.1008 3.7800 3.0269 1.7778 -0.2913;
4.2139 3.8495 3.0761 1.8155 -0.2609];
EP3(:,:,2) = 1.0e+06 * [2.4255 2.6808 2.2051 1.1186 -0.8443;
3.6950 3.5158 2.8300 1.6211 -0.4221;
4.0924 3.7743 3.0225 1.7743 -0.2943;
4.2101 3.8469 3.0741 1.8139 -0.2623];
EP3(:,:,3) = 1.0e+06 * [2.3754 2.6474 2.1799 1.0982 -0.8615;
3.6767 3.5036 2.8207 1.6135 -0.4285;
4.0849 3.7693 3.0187 1.7712 -0.2969;
4.2068 3.8446 3.0724 1.8125 -0.2635];
EP3(:,:,4) = 1.0e+06 * [2.3301 2.6173 2.1571 1.0798 -0.8771;
3.6601 3.4925 2.8123 1.6067 -0.4343;
4.0781 3.7647 3.0152 1.7683 -0.2993;
4.2037 3.8426 3.0708 1.8112 -0.2646];
EP3(:,:,5) = 1.0e+06 * [2.2888 2.5899 2.1364 1.0630 -0.8913;
3.6450 3.4824 2.8046 1.6005 -0.4395;
4.0719 3.7606 3.0120 1.7658 -0.3015;
4.2009 3.8407 3.0694 1.8100 -0.2656];
% dimensions of matrix EP3: [N,k,r]
[Xgrid,Ygrid]=meshgrid(xV_vector,H_D_ratio);
EP3_grid = permute(EP3,[2 3 1]);
figure
hold on
for f=1:N
surf(Xgrid,Ygrid,EP3_grid(:,:,f)'); %,'Color',col(:,:,f));
legendInfo{f} = [num2str(Temp(f)-273.15) ' °C'];
end
xticks(xV_vector);
yticks(H_D_ratio);
title 'EP_3'
xlabel 'x_v (molar fraction of H_2 in V and R) [-]', ylabel 'H/D reactor ratio [-]'
zlabel('EP_3')
legend(legendInfo)
%s.EdgeColor='none';
MATLAB seems to have chosen a poor default view, which fails to illustrate the 3-D nature of the plot. Try adding
view(45,45)
after the plot, and you'll see something closer to what you expect.
Thank you very much! Imposing view(15,15) it works perfectly!
But is it possible to obtain the 4 surfaces in 4 different colors?
The surf command takes a fourth argument, for the color. You could write your loop in a way that uses a different color for each loop iteration.

Sign in to comment.

Products

Release

R2018b

Asked:

on 2 Nov 2019

Commented:

on 2 Nov 2019

Community Treasure Hunt

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

Start Hunting!