C. E.
C. E.

Reputation: 10607

MATLAB: Plot 3D surface from irregular data points

Let's say I have a vector of x coordinates, a matrix of y coordinates and corresponding z values:

xcoordinates = [1 2 3 4 5];
ycoordinates = repmat(xcoordinates,5,1)+rand(5,5);
    z = zeros(5,5);
for x=xcoordinates
    for y=1:5
        z(x,y) = sqrt(x^2+ycoordinates(x,y)^2);
    end
end

How do I plot a surface determined by the z values at the points given by the corresponding x and y values? The first x value defines the x value for all y values in the first row of the matrix, the second x value to all values in the second row and so on.

(If the answer is griddata I would like some additional pointers. How can I get my data into the correct format?)

Upvotes: 0

Views: 490

Answers (1)

aschepler
aschepler

Reputation: 72311

mesh(repmat(xcoordinates,5,1), ycoordinates, z)

By the way, you could easily vectorize this computation:

x = repmat(1:5, 5, 1);
y = x + rand(5,5);
z = sqrt(x.^2+y.^2);
mesh(x', y, z)

Upvotes: 4

Related Questions