Sorting large vector onto 2D grid

조회 수: 5 (최근 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 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