フィルターのクリア

Write for loop results in a matrix

1 回表示 (過去 30 日間)
Danilo M
Danilo M 2018 年 7 月 19 日
コメント済み: Danilo M 2018 年 7 月 19 日
I have an A(:,2) matrix with some x-y coordinates, and B(:,2) matrix with a list of gauge stations coordinates. So, I want to find the nearest gauge station for each line of A. I could get to the code above, but I couldn't find a way to put this in a loop which write the nearest B point for each A line in 'nstation'
dist=bsxfun(@hypot,B(:,1)-A(1),B(:,2)-A(1));
nstation = B(dist==min(dist),:);
  2 件のコメント
Jan
Jan 2018 年 7 月 19 日
Which Matlab version do you use?
Danilo M
Danilo M 2018 年 7 月 19 日
I'm using R2015a version

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

回答 (1 件)

Jan
Jan 2018 年 7 月 19 日
X = B(:,1) - A(:,1).'; % >= R2016b ! Arithmetic expanding
Y = B(:,2) - A(:,2).';
dist2 = sum(X.^2 + Y.^2);
Now you can use this matrix to find the minimal distances. Use pdist for a more efficient calculation, which considers the symmetry.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by