Hello. I have Lat(2689x1) Lon(2689x1) arrays that coordinaines coordinates of meteostations, and lat (1200x200) lon (1200x200) arrays with coordinates of satellite's measurements M(1200x200). I need to find elements of satelletes measurements, that located closer then 0.25 deg to meteostations. What i've did:
k=1;
for m=1:length(Lat)
x=lat-Lat(m);
y=lon-Lon(m);
[a,b]=find(abs((x))<=0.25 & abs((y))<=0.25);
for n=1:length(a)
Mes(k,n)=M(a(n),b(n))
end
St_N(k)=m;
k=k+1;
end;
Because of many loops it works very long. Is there any way to vectorise or speedup this code?

 채택된 답변

darova
darova 2020년 2월 16일

0 개 추천

Try pdist2
D = pdist2([Lat(:) Lon(:)],[lat(:) lon(:)]); % create every possible combinations of distances
[i,j] = find(D<=0.25); % find every distance satisfies condition
% i - station of (Lat,Lon)
% j - station of (lat,lon)
[i1,j1] = ind2sub(j,size(lat)); % return to 2D matrix
Mes = M(i1,j1); % stations that close enough

댓글 수: 5

Eugene Pashinov
Eugene Pashinov 2020년 2월 16일
편집: Eugene Pashinov 2020년 2월 16일
Thanks, but i think it does not work, because:
min(abs((Lat-lat(i1(1),j1(1))))+abs((Lon-lon(i1(1),j1(1)))));
should be less then 0.25, but it is 19
darova
darova 2020년 2월 16일
hm. Can you attach the data?
Eugene Pashinov
Eugene Pashinov 2020년 2월 16일
편집: Eugene Pashinov 2020년 2월 16일
found a mistake, you mixed up places of j and size(lat), correct is:
[i1,j1] = ind2sub(size(lat),j);
Thank you very much for help!
Try without this line:
% [i1,j1] = ind2sub(j,size(lat)); % return to 2D matrix
only
min(abs(Lat-lat(j(1)))+abs(Lon-lon(j(1))))
Don't use ind2sub. It's not correct in this case
% [i1,j1] = ind2sub(j,size(lat)); % return to 2D matrix
Just one index
Mes = M(j); % stations that close enough

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2019b

태그

질문:

2020년 2월 16일

댓글:

2020년 2월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by