Sorting large vector onto 2D grid

5 次查看(过去 30 天)
Hi all,
I would like to sort a vector of measurements onto a 2D grid, by the corresponding latitude and longitude.
I start with a vector of measurements (meas), a vector which lists which row the value corresponds to (row), and a vector which lists which column the value corresponds to (col). 'row' and 'col' are calculated from the latitude and longitude of the raw file data.
If I am using a (360,720) grid, I could do the following:
for i = 1:360
for j = 1:720
index = find(row== i & col == j);
values = meas(index) *NB
end
end
*NB I realise this line would give an error - it is simplified slightly for the sake of this post. Essentially what I do in my actual code here is store the values as a 3D grid (row, col, #measurements).
This is really quite slow. I have been playing with alternatives, but can't quite get my head around how to do this faster. I have been sorting by 'row' and 'col' as in the accepted answer to this question....
This got me close, but I couldn't quite adapt this method for use with two sorts (row and col).
Anyone got any ideas as to how to do this, without using find in a loop. With (360 x 720) grid cells, that's a lot of calls to 'find'!
Thanks,
Dom

采纳的回答

Thorsten
Thorsten 2013-1-24
sz = [360 720];
ind_list = sub2ind(sz, row, col);
for i = 1:360
for j = 1:720
index = 360*(j - 1) + i;
values{i, j} = meas(find(ind_list == index));
end
end

更多回答(1 个)

Thorsten
Thorsten 2013-1-24
sz = [360 720];
ind = sub2ind(sz, row, col);
values = zeros(sz);
values(ind) = meas;
  2 个评论
Initial Conditions
Initial Conditions 2013-1-24
Thorsten,
Thanks for the quick reply. This looks promising. However the end result is a 360 * 720 grid where each cell has one measurement. I should have mentioned each cell can have (and does have) more than one value usually around 10. Could you adapt for this? Is your above code just finding the first instance for each cell?
Many thanks,
Dom
Initial Conditions
Initial Conditions 2013-1-24
To be clearer, this is satellite swath data, so cells can have between 0 and approx 50 measurements per set of 'meas'. I would like to store each measurement at a grid cell - sort of like a stacked 2D array.
Dom

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by