Reputation: 226
I wish to make a 3D plot in matlab using the following data:
x = [1,1,1,1,2,2,2,2,3,3,3,3];
y = [1,2,3,4,1,2,3,4,1,2,3,4];
F = [4,5,6,7,5,6,7,8,6,7,8,9]; % for example
where F=F(x,y) in a function of x and y. (Ie F(1,1) = 4, F(1,2) = 5, F(1,3) = 6, reading down each column.)
To give an idea of the plot I would like to make: If I knew the function F(x,y) analytically I would use the following code:
xvec = [1,2,3];
yvec = [1,2,3,4];
[X,Y] = meshgrid(xvec, yvec);
Fvalues = F(X,Y); % where F = @(x,y) ... has been defined
surf(X,Y,Fvalues);
Upvotes: 0
Views: 112
Reputation: 9075
You are missing a very simple trick here. You are getting 4x3
matrices - X
and Y
. You just have to arrange F
in the proper format and then use surf
command. Write your code as follows:
xvec = [1,2,3];
yvec = [1,2,3,4];
[X,Y] = meshgrid(xvec, yvec);
%reshaping F
F=reshape(F,4,3);
surf(X,Y,F);
Upvotes: 2