Sorting large vector onto 2D grid

4 ビュー (過去 30 日間)
Initial Conditions
Initial Conditions 2013 年 1 月 24 日
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 件のコメント
Initial Conditions
Initial Conditions 2013 年 1 月 24 日
Thorsten,
Will try this code now - but looks good to me. Many thanks.

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by