MATLAB Answers

data analysis - 3D plot - Cartesian product of list

8 views (last 30 days)
laurent jalabert
laurent jalabert on 26 Jul 2018
Commented: Bob Thompson on 31 Jul 2018
Dear all, I have a set of experimental conditions obtained by sweeping 3 parameters A, B, C. For each condition, I have 1 experimental data. The experimental conditions are listed in an order associated with the cartesian product of list of 3 parameters A, B and C, said vector A(1..n), B(1..m), C(1..p). Threfore for any [A(i) B(j) C(k)], I have 1 experimental data located at the index ind=f(i,j,k) in the matrix of cartesian product of list of A,B and C.
With this index, I can get the experimental value M(index,1) for column 1 of my data, for example.
A = [1 2 3]; B = [10 20 30]; C=[100 200 300] ; R is the cartesian product of list of parameters A,B,C and has a length of mxnxp=27 (in this example). It must give this order of row lines of R as : 1 10 100 ; 1 10 200 ; 1 10 300 ; 1 20 100 ; etc... It is worth to notice that meshgrid(A,B,C) does not give row lines listed above but 1 10 100 ; 1 20 100 ; 1 30 100 ; 2 10 100 ; 2 20 100 ; 2 30 100 ; 3 10 100 ; ... this is not what I want.
The ascending index value of R should corresponds to the correct combination of parameters used in the order A then B then C. Therefore the corresponding 'index' of vector M(index,1) corresponds to the row line of R.
Here comes my hurdle for several weeks: I want to display data on 3D graph (using scatter3, plot3, or surf3). I need to define (X,Y,Z,Color).
X can be either A, B or C. Y can be either A, B, or C. Legend Color can be either A, B, or C. Z is the corresponding data located at the index of R. But I need to build up a 2D or 3D vector for Z.
Indeed plot3(A,B,Z,C) is the same than plot3(B,A,Z,C). In case of 3 parameters (A,B,C), the 3D graphs should be displayed as : plot3(A,B,Z,C) , plot3(A,C,Z,B), plot3(B,C,Z,A).
How to build up the matrix Z by picking up the correct index of R in order to display each 3D graphs?
(I am sorry, I spent a long time to synthetise my question, but it helped me to better define my problem. Still, I feel that for readers, it could be not enough clear. I am so sorry for that).
My final goal is to be able to solve this problem for N parameters (N=1 to 5). The data are recorded in a fixed order (A,B,C,...).
I will be more than happy to get any answer, at least before my head will explose ! Sincrely yours Laurent


Sign in to comment.

Answers (1)

Bob Thompson
Bob Thompson on 27 Jul 2018
I could be totally wrong, but I think it's pretty simple. Just need a loop for your color parameter. Suppose we build plot3(A,B,Z,C).
hold on
for c = 1:size(data,3) % Loop through C elements
z = data(:,:,c);
... % Add other plot things here.
Looping through one of the other parameters would be very similar.
for b = 1:size(data,2);
z = data(:,b,:);


Show 6 older comments
Bob Thompson
Bob Thompson on 30 Jul 2018
Have you tried using mesh()? Basic form is mesh(X,Y,Z) where Z is 2D with [length(Y),length(X)] = size(Z).
I'm still a little unsure why you're remaking Z. I understand that you're trying to reshape it into a 2D vector, but didn't you already have Z as a 3D vector (A,B,C)? If this is the case, than for each C value, Z(:,:,C) is the same thing as what you're creating in shape_Z.
laurent jalabert
laurent jalabert on 31 Jul 2018
Well, I tried meshgrid, but I could not understand well. Also in your answer, I still don't understand why it is [length(Y) length(X) and not [length(X) length(Y)] which should be more intuitive. But what is working is the following code :
X = (-1:1:5)'; sx = size(X)
Y = (-1:1:10)'; sy = size(Y)
Z = sin((0:1:sx(1)*sy(1)-1))'; size(Z)
shape_Z = reshape(Z, length(X), length(Y)); size(shape_Z)
alpha 0.5
Still, I cannot understand why it works, but it works. surf(X,Y,shape_Z') works also. So ... nothing very intuitive. Thanks again for your valuable help and suggestions.
Bob Thompson
Bob Thompson on 31 Jul 2018
Hey, if it works, it works. Understanding is good though.
I used [Y, X] instead of [X, Y] because that is how it is defined by the function, not my personal preference. The same applies for the dimensions of Z used in surf.
surf(Z) plots Z in a 3D surface with X and Y values set as integers for each row and column of Z. surf(X,Y,Z) plots Z in a 3D surface with X and Y values set as corresponding rows and columns of Z. I.e. if X = [1 2 3] and Y = [10 20 30] and you use surf(Z) then Z(1,1) will be plotted at the (x, y) coordinate of (1,1) because no X and Y were specified in surf. But if you use surf(X, Y, Z) then Z(1,1) will be plotted at (1, 10), to properly correspond to the X and Y arrays specified.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by