How to vectorise or speedup the code

1 回表示 (過去 30 日間)
Eugene Pashinov
Eugene Pashinov 2020 年 2 月 16 日
コメント済み: darova 2020 年 2 月 16 日
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 日
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 件のコメント
darova
darova 2020 年 2 月 16 日
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))))
darova
darova 2020 年 2 月 16 日
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 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by