Creating a matrix from spaced out lines of another matrix

I have a large 3 column matrix of xyz elevation data in 1 meter intervals. The data corresponds to a 100 meter by 100 meter grid, and lists the data row by row, meaning that the y term changes by 1 every 100 values. The data begins in the top right corner of the 'grid', as shown:
x y z
0 100 34
1 100 35
2 100 23
...
0 99 54
1 99 45
2 99 37
...
etc.
I would like to write a function that creates a smaller matrix corresponding to a 3 meter by 3 meter portion of the grid, but because of the large matrix's construction, the nine lines of data are grouped in three sets of three, each spaced around 100 lines from the next. Any advice and assistance would be appreciated.

2 Comments

Is your matrix stored in a numeric data type (such as a double), or in a cell array, table, or other data type?
So, you want to enter the center coordinates (x,y) -- suppose they are 43, 52 -- and then get output
42 51 72
43 51 77
44 51 68
42 52 72
43 52 71
44 52 62
42 53 72
43 53 77
44 53 63
?
The data is stored as a double.
The eventual goal is to plot the 3x3 grid on a mesh, but what you're suggesting as input and output would be really helpful, yes.

Sign in to comment.

 Accepted Answer

Here is a function definition. Put it in a file named smallFromBig.m.
function [xyzSmall] = smallFromBig(x,y,xyzBig)
isNearX = ismember(xyzBig(:,1),[x-1 x x+1]);
isNearY = ismember(xyzBig(:,2),[y-1 y y+1]);
xyzSmall = xyzBig(isNearX & isNearY,:);
end
Here is an example of how I called it.
% Make up some input data
m = 100;
n = 100;
xyzBig = [repelem(1:m,n)' mod(0:m*n-1,n)' rand(m*n,1)];
% Find the small grid around x==47, y==52
[xyzSmall] = smallFromBig(47,52,xyzBig)

3 Comments

Thanks so much! How would I edit the size of the small grid? I might be misinterpreting the code, but changing the isNear variables to [x- 2 x-1 x x+1 x+2] and then [x-2 x x+2] aren't working.
I'm not sure why your version is working, but I would do it like this:
xd = 2;
yd = 3;
isNearX = ismember(xyzBig(:,1),[x-xd:x+xd]);
isNearY = ismember(xyzBig(:,2),[y-yd:y+yd]);
xyzSmall = xyzBig(isNearX & isNearY,:);
which would give a 5x7 grid, for example.
That makes sense. It works great, thanks.

Sign in to comment.

More Answers (1)

Try this
dx = max(x)-min(x);
ix = round((x-min(x))/max(x)*2) + 1; % result 1 2 3
ind = cell(3,1);
for i = 1:length(ind)
ind{i} = x(ix==i); % write data
end

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!