Using 3D array to subtract row Q from row P

1 回表示 (過去 30 日間)
Sara Foster
Sara Foster 2019 年 9 月 7 日
コメント済み: Sara Foster 2019 年 9 月 7 日
If I have a test array e.g C = [100,100,100;50,50,50;40,40,40] and I use the command P = C(1,1,:), it will give me a value of 100 (which is the first P1 pixel) and using Q = C(3,1,:) it gives me a value of 40.
However I want to use an example function like this to calculate distance between pixels:
P = P(1); P2 = P(2); P3 = P(3);
Q = Q(1); Q2 = Q(2); Q3 = Q(3);
squaredDistance = (P1-Q1)^2 + (P2-Q2)^2 + (P3-Q3)^2;
And P1 and Q1 using squared distance formula only gives me a value of 3600, where I want total squared distance using P2 and P3 & Q2 and Q3 is there any way of doing this without needing to resort to for loops? Thanks!

回答 (1 件)

James Tursa
James Tursa 2019 年 9 月 7 日
編集済み: James Tursa 2019 年 9 月 7 日
It is unclear what you really want. If you want the Euclidean distance squared between rows, e.g., rows 1 and 3, then just
d = C(1,:) - C(3,:);
result = d * d';
  1 件のコメント
Sara Foster
Sara Foster 2019 年 9 月 7 日
If I'm using P and Q as input commands, I want to use any row value for these two inputs e.g. I could have Q = C(3,1,:) or Q = C(2,1,:) depending on the input I choose for Q, as my inputs are P and Q and output is the distance.

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

カテゴリ

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