I have 3 vectors of data, X (position), Y (position) both of which are not regularly spaced, and Z(value of interest at each location). I tried contourf, which doesn't work because it needs a matrix for Z input.
You can also use griddata.
%Generate random data
x = rand(30,1);
y = rand(30,1);
z = rand(30,1);
%Create regular grid across data space
[X,Y] = meshgrid(linspace(min(x),max(x),n), linspace(min(y),max(y),n))
%create contour plot
contour(X,Y,griddata(x,y,z,X,Y))
%mark original data points
hold on;scatter(x,y,'o');hold off
For a contour plot you actually need either a matrix of z values, or a set (vector) of z-values evaluated on a grid. You cannot define contours using isolated Z values at (X,Y) points on the grid (i.e. what you claim you have).
You need to have the generating process (or function) provide values for a grid of (x,y) points.
If not, then you can create a surface from nonuniform data as #nate correctly pointed out, and then draw the contours on that surface.
Consider the following (random) example:
N = 64; % point set
x = -2 + 4*rand(N,1); % random x vector in[-2,2]
y = -2 + 4*rand(N,1); % random y vector in[-2,2]
% analytic function, or z-vector
z = x.*exp(-x.^2-y.^2);
% construct the interpolant function
F = TriScatteredInterp(x,y,z);
t = -2:.25:2; % sample uniformly the surface for matrices (qx, qy, qz)
[qx, qy] = meshgrid(t, t);
qz = F(qx, qy);
contour(qx, qy, qz); hold on;
plot(x,y,'bo'); hold off
The circles correspond to the original vector points with values (x,y,z) per point, the contours on the contours of the interpolant surface.
Related
I have two arrays
x = [0 9.8312 77.1256 117.9810 99.9979];
y = [0 2.7545 4.0433 5.3763 5.0504];
figure; plot(x, y)
I want to make more samples of x and y then I interpolated both arrays. I tried this code
xi =min(x):.1:max(x);
yi = interp1(x,y,xi);
figure; plot(xi, yi)
but the trajectory is not same as previous plot. Its because the xi is not fluctuating same as x. How should I interpolate both arrays with same trajectory as original one?
This is an issue because when interpolating, MATLAB is going to ignore the order that you feed in the points and instead just sort them based upon their x location.
Rather than interpolating in x/y coordinates, you can instead use a parameter which represents the cumulative arc length of the line segments and use that to interpolate both the x and y coordinates. This will provide you with an interpolant that respects the order and guarantees monotonicity even for multiple values at the same x coordinate.
% Compute the distance between all points.
distances = sqrt(diff(x).^2 + diff(y).^2);
% Compute the cumulative arclength
t = cumsum([0 distances]);
% Determine the arclengths to interpolate at
tt = linspace(t(1), t(end), 1000);
% Now interpolate x and y at these locations
xi = interp1(t, x, tt);
yi = interp1(t, y, tt);
if the function F is available it is easy to draw surf plot i.e.
x=1:0.1:4;
y=1:0.1:4;
[X,Y]=meshgrid(x,y);
Z=sin(X).^2+cos(Y).^2;
surf(X,Y,Z);
view(2) ;
in my case I calculated F function using least square:
for example I have x and y vectors
x = [0 9.8312 77.1256 117.9810 99.9979];
y = [0 2754.5 4043.3 5376.3 5050.4];
the linear function of these two vector is define by
F= [1149.73 , 37.63];
therefore the estimation is equal to
z= [ones(5,1) x']*F';
which is
z = [1149.73 1519.67 4051.96 5589.35 4912.65];
and if it is plotted
plot(x,y,'b.');
hold on;plot(x,y,'b-');
hold on; plot(x,z,'-r');
The linear z ( red line) is showing correctly. Now I want to plot it for all possible combination of x and y using grid and I need to have a mesh for all inputs
[X,Y] = meshgrid(x,y);
but how to make the Z matrix to show the intensity plot of function Z? The Z suppose to have high intensity close to z value and less value far from it. I should suppose to get something like this
Thanks
P.S: the F is calculated using pinv (least square).
You have to interpolate the scattered data to plot it on grid. Here is a simple example for your x, y and z vectors
xi=linspace(min(x),max(x),100)
yi=linspace(min(y),max(y),100)
[XI YI]=meshgrid(xi,yi);
ZI = griddata(x,y,z,XI,YI);
contourf(XI,YI,ZI)
Following a previous question that was asked, I wanted to create a 3d volume, i,e, f(x,y,z), starting from a 2D matrix I(x,y) and not just a curve.
For example, assume I=peaks(10), how can I revolve it around one of the axes (say the y-axis), to get a 3D matrix? Will it be easier if I instead have I(r,theta)?
I can rotate a plane in 3D, but this will not be part of a 3D matrix, just new x,y,z coordinates.
I think it's pretty straightforward to check the indices of your expected 3d array, find geometrically which I(x,y) value they correspond to (yes, essentially with a Cartesian to cylindrical transformation), then filling up each value as necessary.
Here's a minimal version of what I mean. It is assumed that the input array of size [N,N] corresponds to coordinates x, y in 0:N-1. The input array is assumed to be oriented along the xz plane, and it is rotated about the z axis in order to generate the output array V. The dimensions of V thus correspond to -(N-1):N-1 along x and y, and 0:N-1 along z. Thus the dimensions of V are [2*N-1, 2*N-1, N].
Demonstrating two approaches (using griddata and interp2, respectively), complete with plotting for reproducibility:
% size of the problem
N = 10;
% input data
I = peaks(N);
% two sets of output V for two methods
V1 = zeros(2*N-1,2*N-1,N);
V2 = zeros(2*N-1,2*N-1,N);
[i1,i2,i3] = ndgrid(1:2*N-1,1:2*N-1,1:N);
% [i1(:), i2(:), i3(:)] are the contiguous indices of V
% z dimension is the same as of I: rotate around z axis
% it will be assumed that input 1:N span elements from 0 to N-1
% output V spans -(N-1):N-1 along x and y
x = i1-N; % -(N-1):N-1
y = i2-N; % -(N-1):N-1
z = i3-1; % 0:N-1
% input array I is in xz plane, rotated along z axis, geometrically speaking
% identify the cylindrical coordinates of each voxel [i1,i2,i3]
[~,r_out,z_out] = cart2pol(x,y,z); % theta is redundant; z_out===z
% identify the coordinates of each input pixel with the above
[j1,j2] = meshgrid(1:N,1:N);
r_in = j1-1; % Cartesian input x <-> cylindrical output r
z_in = j2-1; % Cartesian input y <-> cylindrical output z
% note that j1 and j2 are swapped with respect to x and y
% but this is what interp2 will expect later
% interpolate each voxel based on r and z
method = 'nearest'; %probably the least biased
%method = 'cubic'; %probably the prettiest
V1(:) = griddata(r_in,z_in,I,...
r_out(:),z_out(:),method);
V2(:) = interp2(r_in,z_in,I,...
r_out(:),z_out(:),method,...
0); % extrapolation value, otherwise NaNs appear outside
% plot two slices: xz plane and general rotated one
figure;
% generate rotated versions of the xz plane by rotating with phi around z
for phi=[0, -25, -90]/180*pi
[xp0,zp] = meshgrid(-(N-1):0.1:N-1,0:0.1:N-1);
xp = xp0*cos(phi);
yp = xp0*sin(phi);
subplot(121);
slice(y,x,z,V1,xp,yp,zp);
title('griddata nearest');
shading flat;
axis equal vis3d;
hold on;
subplot(122);
slice(y,x,z,V2,xp,yp,zp);
title('interp2 nearest, extrap 0');
shading flat;
axis equal vis3d;
hold on;
end
As you can see, slice directly plots the data in V, so this is an accurate representation of the resulting 3d array.
For reference, here is a single specimen of the input I = peaks(10):
I'm using the interp1 function to interpolate some points on this graph
My problem is that I want the new points to be equidistant. But in the interp1 function the input arguments are the x(before) , y(before) and the x(new) which is the vertical coordinate and not the contour distance.
My question is if there is any other function which solves my problem? If not, does anyone know how can I transform the x-vector?
EDIT:
an example with my problem is here:
x=0:0.1:10;
y=x.^4;
xx=linspace(min(x),max(x),10);
yy=interp1(x,y,xx);
hold on;
plot(x,y);
plot(xx,yy);
plot(xx,yy,'ro');
hold off;
You can do this by reformulating your curve as a parametric function of length along the curve. What you want is for the final points (where you interpolate) to have equal length between them. It's possible to do this by approximating the 'true' curve as a piecewise linear curve that connects the original data points.
Say we have some data points in matrix xy, where each row is a point and the x/y coordinates are on columns 1/2:
% make some fake data
% triple exponential function has nonuniform spacing
x = linspace(.4, .8, 20)';
y = exp(exp(exp(x)));
x = (x - min(x)) ./ range(x);
y = (y-min(y)) ./ range(y);
xy = [x, y];
Find the length of each point along the curve, starting from 0 at the first point:
% distance of each point from previous point
% note that points need to be in order
ds = [0; sqrt(sum(diff(xy, 1, 1) .^ 2, 2))];
% integrate to find length along the curve
s = cumsum(ds);
Now, consider both x and y to be a function of s. Interpolate x and y at a set of equally spaced lengths along the curve:
% find a set of equally spaced lengths along the curve
si = linspace(s(1), s(end), 20)';
% interpolate x and y at these points
xyi = interp1(s, xy, si);
Verify that the solution works:
% distance between successive interpolated points
% they should all be equal
sqrt(sum(diff(xyi, 1, 1) .^ 2, 2)
Original data:
Interpolated:
I have points in 3D space and their corresponding 2D image points. How can I make a mesh out of the 3D points, then texture the triangle faces formed by the mesh?
Note that the function trisurf that you were originally trying to use returns a handle to a patch object. If you look at the 'FaceColor' property for patch objects, you can see that there is no 'texturemap' option. That option is only valid for the 'FaceColor' property of surface objects. You will therefore have to find a way to plot your triangular surface as a surface object instead of a patch object. Here are two ways to approach this:
If your data is in a uniform grid...
If the coordinates of your surface data represent a uniform grid such that z is a rectangular set of points that span from xmin to xmax in the x-axis and ymin to ymax in the y-axis, you can plot it using surf instead of trisurf:
Z = ... % N-by-M matrix of data
x = linspace(xmin, xmax, size(Z, 2)); % x-coordinates for columns of Z
y = linspace(ymin, ymax, size(Z, 1)); % y-coordinates for rows of Z
[X, Y] = meshgrid(x, y); % Create meshes for x and y
C = imread('image1.jpg'); % Load RGB image
h = surf(X, Y, Z, flipdim(C, 1), ... % Plot surface (flips rows of C, if needed)
'FaceColor', 'texturemap', ...
'EdgeColor', 'none');
axis equal
In order to illustrate the results of the above code, I initialized the data as Z = peaks;, used the built-in sample image 'peppers.png', and set the x and y values to span from 1 to 16. This resulted in the following texture-mapped surface:
If your data is non-uniformly spaced...
If your data are not regularly spaced, you can create a set of regularly-spaced X and Y coordinates (as I did above using meshgrid) and then use one of the functions griddata or TriScatteredInterp to interpolate a regular grid of Z values from your irregular set of z values. I discuss how to use these two functions in my answer to another SO question. Here's a refined version of the code you posted using TriScatteredInterp (Note: as of R2013a scatteredInterpolant is the recommended alternative):
x = ... % Scattered x data
y = ... % Scattered y data
z = ... % Scattered z data
xmin = min(x);
xmax = max(x);
ymin = min(y);
ymax = max(y);
F = TriScatteredInterp(x(:), y(:), z(:)); % Create interpolant
N = 50; % Number of y values in uniform grid
M = 50; % Number of x values in uniform grid
xu = linspace(xmin, xmax, M); % Uniform x-coordinates
yu = linspace(ymin, ymax, N); % Uniform y-coordinates
[X, Y] = meshgrid(xu, yu); % Create meshes for xu and yu
Z = F(X, Y); % Evaluate interpolant (N-by-M matrix)
C = imread('image1.jpg'); % Load RGB image
h = surf(X, Y, Z, flipdim(C, 1), ... % Plot surface
'FaceColor', 'texturemap', ...
'EdgeColor', 'none');
axis equal
In this case, you have to first choose the values of N and M for the size of your matrix Z. In order to illustrate the results of the above code, I initialized the data for x, y, and z as follows and used the built-in sample image 'peppers.png':
x = rand(1, 100)-0.5; % 100 random values in the range -0.5 to 0.5
y = rand(1, 100)-0.5; % 100 random values in the range -0.5 to 0.5
z = exp(-(x.^2+y.^2)./0.125); % Values from a 2-D Gaussian distribution
This resulted in the following texture-mapped surface:
Notice that there are jagged edges near the corners of the surface. These are places where there were too few points for TriScatteredInterp to adequately fit an interpolated surface. The Z values at these points are therefore nan, resulting in the surface point not being plotted.
If your texture is already in the proper geometry you can just use regular old texture mapping.
The link to the MathWorks documentation of texture mapping:
http://www.mathworks.com/access/helpdesk/help/techdoc/visualize/f0-18164.html#f0-9250
Re-EDIT: Updated the code a little:
Try this approach (I just got it to work).
a=imread('image.jpg');
b=double(a)/255;
[x,y,z]=peaks(30); %# This is a surface maker that you do have
%# The matrix [x,y,z] is the representation of the surface.
surf(x,y,z,b,'FaceColor','texturemap') %# Try this with any image and you
%# should see a pretty explanatory
%# result. (Just copy and paste) ;)
So [x,y,z] is the 'surface' or rather a matrix containing a number of points in the form (x,y,z) that are on the surface. Notice that the image is stretched to fit the surface.