I have some values and the location of them on a grid.
e.g.
V1=1
V2=2
V3=4
...
I know the location of those values on a fixed spaced grid.
e.g.
V2 x x V1 x V3
x x x x x x
V5 x x x V4 V6
Now what I need is to interpolate the missing values x.
e.g. the first row
2 1.66 1.33 1 2.5 4
It's a two dimensional problem. Any hints how I can efficiently solve it? The amount of V may vary.
Thanks
Simple interpolation of irregular grid problem. meshgrid is useful.
x = [4,1,6,5,1,6];
y = [1,1,1,3,3,3];
v = [1,2,4,2,4,5];
[xq,yq] = meshgrid(1:max(x), 1:max(y));
vq = griddata(x,y,v,xq,yq);
You need to explicitly define the x- and y- positions (this case row and column numbers) of your V data. Then use meshgrid to generate a grid (this case the matrix itself). Then use griddata to interpolate the data over the grid that you just created. vq is the resulting matrix you want.
Related
I am attempting to plot 3D surfaces in MATLAB, and I made use of meshgrid, similar to what the MATLAB tutorials said here: http://www.mathworks.com/help/matlab/ref/meshgrid.html
I wrote a very simple three line script that I believed would produce the surface z = x + y and it is as follows:
[x , y] = meshgrid( linspace( 0 , 10 , 10 ) , linspace( 0 , 10 , 10 ) );
z = x + y;
surf( [ x , y , z] );
From what I understand, line 1 produces all combinations of (x,y) coordinates evenly spaced from 0 to 10. Then line 2 just applies the formula z = x + y to that exhaustive list of combinations. Then line 3 just plots all the (x, y, z) points.
But I got the following "thing" as output:
I'm pretty sure the graph in the above picture is not z = x + y, and I have no clue why there aren't two axes going up to maximum value 10.
Still, I find the script too simple and couldn't see anything wrong with it. Could anyone point out where I overlooked something? Thank you.
Your syntax for generating the 3D coordinates is right. Your call to surf is incorrect. What you actually need to do is separate x, y and z into three separate parameters:
surf(x,y,z);
When you do that, you get this surface. Take note that the figure generated was using MATLAB R2013a, so the colour map shown is not the parula colour map that is available as of R2014b and up, but the surface will be the right one that is what you're looking for:
... now why do you need to separate your x, y and z points to create the surface? The reason why is because doing [x,y,z] means that you are concatenating the x, y and z coordinates into a single 2D signal, and so what's happening is that you are creating a 2D signal that is 10 x 30. Calling surf with this single 2D array automatically assumes that the x values span from 1 to 30 and the y values span from 1 to 10 and those are the 2D grid of values that span the axis of your surf plot in conjunction with the z values shown, where the z values originate from the concatenated matrix created earlier. If you look at the plot you generated, you can see the x values are spanning from 1 to 30, and that's obviously not what you want.
You need to separate the x, y and z values to achieve the desired plane.
I have 3 dimensional (X-Y-Z) data which includes-
11 Y vectors (100*1 each (range 0:0.01:1)) each is unique and correspond to the same X vector (100*1 (range 0:0.01:1)). Thus, Y can be treated as matrix with dimension 100*11.
Different Y vectors correspond to different Z values on Z axis (11 values of Z ranging from 0 to 1).
e.g. => Every value of Z on Z axis i.e. Z1=0.05 ... Z11=0.95 has different Y vector for the same X vector. Thus it is like: X-Y1-Z1, X-Y2-Z2, X-Y3-Z3...X-Y11-Z11 (Z1 to Z11 are fixed values like 0.05, 0.1 ,..., 0.95 which are repeated equal to the length of X and Y).
I have already plotted this data as 11 discrete contour lines stacked one above other in 3D space using plot3 hold on. But, I want to create surface out of it. I think I can use interpolation using griddata but I didn't understand how to approach this problem. Can anybody help?
I have a file with data arranged in three columns. I am trying to make 2D contour plot of these values, where the values in the third column (Z) is projected on the space formed by values in the first (X) and second column (Y). But usual matlab commands like 'contour' and 'imagesc' take the Z-values in the matrix format. Is there a way out in Matlab to plot these values in a 2D-plane?
Contour usually works with two vectors (X and Y) and a matrix (Z). So for each elements of the two vectors (X(i) , Y(i)), there should be a value in the matrix (Z(i,j)). Thus the size of the matrix Z should be equal to the size of the first vector (X) multiplied by the size of the second vector (Y).
if the x,y,z have the same size then you can do something like this:
[X,Y,Z] = meshgrid(x,y,z);
contour(X,Y,Z)
on the other hand if you manage to make the sizes correct then you can do something like this example:
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);
figure
contour(X,Y,Z)
I am charting the following data:
a=[...
0.1, 0.7, 0.00284643369242828;...
0.1, 0.71, 0.00284643369242828;...]
such that column 1 never surpasses approximately 10
also such that column 2 goes from .7 to 1.
Column 3 seems ok
When i chart my surface using surf(a) it looks like this:
it appears not to be properly considering what should be x and y.
anything seem weird there?
I think you need to try one of two things: either break out your height column into its own rectangular matrix Z and use surf(Z) to plot each point relative to its location in the matrix (so your x- and y-axes will not be scaled the way you want), or you can put your desired x- and y-coordinates in their own vectors, and plot the matrix Z (defined at every point (xi, yj) for all i in N and j in M where x is N elements long and y is M elements long) with surf(x,y,Z).
x = 0.1:0.1:10; % or whatever increment you need
y = 0.7:0.01:1; % or whatever increment you need
Z = zeros(length(x),length(y); % initialized to the correct size, fill with data
I think you are going to have to regenerate your Z-data so that it is in a rectangular matrix that is (elements in x) by (elements in y) in dimension.
EDIT: You do not need to recreate your data. If you know that you have n unique elements in x and m unique elements in y, then you can use:
X = reshape(data(:,1),m,n);
Y = reshape(data(:,2),m,n);
Z = reshape(data(:,3),m,n);
surf(X,Y,Z);
And that should give you what you are looking for.
I am trying to create a 2-D grid from a vector.
So, for example I have:
x = 1:1:10;
z = 2:2:20;
Now, I want to create a grid which has x on both side of the grid cell and z as grid cell value.
I tried doing it as :
[X,Y] = meshgrid(x, x);
newZ = griddata(x, x ,z, X, Y);
But this gives me error:
The underlying triangulation is empty - the points may be
collinear.
Need help solving this.
In a high level, griddata() takes a 2d surface with variable z-value at each point as the first part of the input, and the query points as the second part of the input. To be more specific, when we look into the definition of the function:
vq = griddata(x,y,v,xq,yq)
x and y specifies the range of x and y values, v is like z-value in a plane, and xq and yq together are query points. Here, v (in your case, z) is expected to be a 2d matrix, to be more specific, the size of v is [length(x), length(y)], whereas in your case, you put z as a vector. Matlab generates the warning since the size doesn't match.
For your reference: http://www.mathworks.com/help/matlab/ref/griddata.html?refresh=true