Lionel
Lionel

Reputation: 331

How do I construct an Esri grid?

I read a lot of information about this subject but I can't obtain a solution about my problem.

First, I have a file with 3 columns: X Y Z

In MATLAB, I did this:

data = load('data.txt');
X = data(:,1);
Y = data(:,2);
Z = data(:,3);

This file is like this:

7037    6032    3
7036    6028    5
7037    6029    4
7037    6030    3
7038    6031    6
7039    6031    2
7037    6033    7

And I want to obtain the following matrix from the above matrix:

5   NaN NaN NaN NaN NaN
NaN 4   3   NaN 3   7
NaN NaN NaN 6   NaN NaN
NaN NaN NaN 2   NaN NaN

The rules is that the first column Y(1) = min(Y) , the second column Y(2) = Y(1) + 1. The first line is X(1) = min(X), X(2) = X(1) + 1. Essentially, the first column acts as a row index, the second column acts as a column index, and for each row and column pair, the third column gets mapped to a location in this matrix. As such, the output matrix will be like so: out(1,1)=X(1) Y(1) ; out(1,2) = X(1) Y(2)

At the start, I think about created a matrix out like so:

xr = sort(unique(X));
yr = sort(unique(Y));
a = length(xr);
b = length(yr);
out = NaN(a,b);

After, with a loop, put I place this data onto this out matrix, but this obviously doesn't work.

For more information on an Esri grid, here's a Wikipedia article about it. The example grid in that page is what I desire. http://en.wikipedia.org/wiki/Esri_grid

Upvotes: 0

Views: 92

Answers (1)

rayryeng
rayryeng

Reputation: 104565

I now understand what you want. The link that you posted from Wikipedia is very useful. You are trying to build what is known as an Esri grid. Here is a pictorial representation found on Wikipedia:

enter image description here

What you are given is a N x 3 matrix where the first column denotes the row IDs of this matrix, the second row denotes the column IDs of this matrix, and the third column denotes the values at each pair of IDs. So for example, given the example above - specifically looking at the right of the figure, your text file could look like:

275 125 5
275 175 2
...
...
25 75 5
25 125 1

Each row consists of a row index, a column index and a value that maps to this location in the grid. You had the right approach in that you should use unique - specifically the third output. We need to obtain a unique ID for the first two columns of your data independently. Once we do this, I'm going to show you the very powerful accumarray function. We are basically going to use the unique IDs found in the previous step, and we use these to index into our grid and place each value that corresponds to each unique pair of row and column IDs into this grid. Therefore, your code is very simply:

data = load('data.txt');
%// Or you can do this for reproducing the results
%data = [7037 6032 3;
%7036 6028 5;
%7037 6029 4;
%7037 6030 3;
%7038 6031 6;
%7039 6031 2;
%7037 6033 7];

[~,~,id1] = unique(data(:,1));
[~,~,id2] = unique(data(:,2));
out = accumarray([id1 id2], data(:,3), [], [], NaN);

out produces the desired Esri grid, and we get:

out =

     5   NaN   NaN   NaN   NaN   NaN
   NaN     4     3   NaN     3     7
   NaN   NaN   NaN     6   NaN   NaN
   NaN   NaN   NaN     2   NaN   NaN

So how does this work? accumarray accepts in a matrix of row and column locations that you want to use to access the output. At each of the corresponding row and column locations, you provide a value that gets mapped to this bin. Now, by default accumarray sums up the values that get mapped to each bin, but I'm going to assume that your values in your text file are all unique in that only one value gets mapped to each row and column index. Therefore, we can certainly get away with the default behaviour, and so you'd specify a [] for this behaviour (fourth input). Therefore, we will use the last column of your matrix as the values that get put into this matrix, use the [] input to allow accumarray to infer the size of your matrix (third input), then any values that don't get mapped to anything, we will fill this in with NaN. We aren't going to sum anything.

With the above explanation, the code follows.

Upvotes: 2

Related Questions