2

I have a 2 Dimensional input data; a set of vector with 2 components, let's say 200. And for each one of those I have a scalar value given to them.

So it's basically something like this:

{ [input1(i) input2(i)] , output(i) } where i goes from 1 to 200

I would like to make a 3 Dimensional plot with this data, but I don't know how exactly. I have tried with surf. I have done a meshgrid with the input value, but I don't know how to obtain a matrix out of the output data in order to do a surf.

How can I get a 3 Dimensional plot with this data?

HebeleHododo
  • 3,620
  • 1
  • 29
  • 38
Simon
  • 4,999
  • 21
  • 69
  • 97
  • Do your inputs vary linearly (e.g. 10, 20, 30...) or are they 'randomly' distributed (e.g. 20, 3.4, -5...)? – Bill Cheatham Nov 05 '10 at 10:40
  • 2
    Related questions: [How Do I Generate a 3-D Surface From Isolines?](http://stackoverflow.com/questions/1672176/how-do-i-generate-a-3-d-surface-from-isolines), [How do I make a surf plot in MATLAB with irregularly spaced data?](http://stackoverflow.com/questions/2848015/how-do-i-make-a-surf-plot-in-matlab-with-irregularly-spaced-data) – gnovice Nov 05 '10 at 15:46

1 Answers1

5

Assuming your input data is 'randomly' spaced:

>> inputs = randn(400, 2);
>> outputs = inputs(:, 1) .* inputs(:, 2);   % some function for the output

You could simply plot a scatter3 plot of these data:

>> scatter3(inputs(:, 1), inputs(:, 2), outputs)

alt text

But a better way is to interpolate, using TriScatteredInterp so you can plot the underlying function as a surface:

% create suitably spaced mesh...
gridsteps_x = min(inputs(:, 1)):0.5:max(inputs(:, 1));
gridsteps_y = min(inputs(:, 2)):0.5:max(inputs(:, 2));
[X, Y] = meshgrid(gridsteps_x, gridsteps_y);

% Compute function to perform interpolation:
F = TriScatteredInterp(inputs(:, 1), inputs(:, 2), outputs);

% Calculate Z values using function F:
Z = F(X, Y);

% Now plot this, with original data:
mesh(X, Y, Z); 
hold on
scatter3(inputs(:, 1), inputs(:, 2), outputs);
hold off

alt text

Bill Cheatham
  • 11,396
  • 17
  • 69
  • 104