How to change the size of a matrix by filling with nan's?

12 views (last 30 days)
Hello
I have x, y and z matrices relating to lat,long and depth of bathymetric surveys. I have this data for several years although each survey is in a slightly different position.
Therefore to make consistent plots and animations I need to first define one grid of x and y matrices which all the z matrices can relate to?
I am thinking if I take the smallest and largest eastings and northing from the x and y matrices and then create a new X and Y with these ranges I can use these to plot the different z values on however first I need to make z the same correct size but I want to do this by adding NaN's around the data so the survey area still remains unchanged but I can contour plot x,y,z and x,y,z1 and x,y,z2 etc... so that the coordinates are consistent.
I know this is not very clear but I am struggling to get my head round it at the moment! Any help would be much appreciated.
Thank you
  3 Comments
scour_man
scour_man on 29 Jun 2011
x1=[50.6013:5.0292e-005:50.6088]';
y1=[-3.4032:1.0969e-004:-3.3868]';
x2=[50.6008:5.3615e-005:50.6088]';
y2=[-3.4044:1.1811e-004:-3.3867]';
z1 and z2 are both 150x150 matrices of the bed depths. A large area of each matrix is NaNs, but both matrices contain different values in slightly different locations. For example z1 might look something like:
[NaN NaN NaN NaN -1 -1 -3 NaN NaN;
NaN NaN -3 -3 NaN NaN NaN NaN NaN;
NaN NaN -3 -4 -5 NaN NaN NaN NaN;
NaN NaN NaN NaN NaN NaN NaN NaN NaN]
...etc but 150x150. z2 will be similar but with the depth values in slightly different positions. (each different z matrix is survey data from a different year).
In order to create animations of all years I need one X and one Y matrix which all Z matrices can be plotted on if this makes sense?
I understand I can find the smallest and largest x and y values and then create a Z1 matrix full of NaNs the same size as X,Y and then insert the actual z1 data into that. This is time consuming and fiddly because I need to try and work out how to position the data correctly as this is of utmost importance. I want to do this for several years of data.
Am I doing this correctly / is there a better way?
Thanks!!
scour_man
scour_man on 29 Jun 2011
[r c]=find(~isnan(z3),1,'first'); % Finds first non nan position
STARTX=((((max(x3)-min(x3))/150))*c)+(min(x1))
STARTY=((((max(y3)-min(y3))/150))*r)+(min(y1))
INTX=(max(X)-min(X))/160; % Interval of X (in this case X is 160x1)
INTY=(max(Y)-min(Y))/163; % Interval of Y (in this case Y is 163x1)
a=(STARTX-min(X))/INTX; % a and b define the x,y position for z3 to start in Z
b=(STARTY-min(Y))/INTY;
clearvars STARTX STARTY INTX INTY
Z3=nan(163,160); % Creates matrix of specified size full of nans
Z3(((round(b))-r+1):((round(b))-r+150),((round(a))-c+1):((round(a))-c+150))=z3; % Pastes z3 into Z3 at correct position
clearvars a b r c

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 28 Jun 2011
Preempt z with NaNs and then fill with data
z=nan(10,10)
z(2:6,3:7)=magic(5)

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!