フィルターのクリア

Rearranging matrix based on coordinates

1 回表示 (過去 30 日間)
Elise
Elise 2015 年 7 月 7 日
コメント済み: Andrei Bobrov 2015 年 7 月 7 日
I have three matrixes, one containing x values and one for y values of trajectories. I also have a matrix with mass corresponding to these location. I know want to sort the mass values into a new matrix based on the coordinates, filling the empty spots with zeros.
Example.
x = [1, 2, 3; 1, 2, 3; 1, 2, 3]
y = [1, 2, 2; 1, 1, 3; 3, 2, 1]
mass = [1, 12, 0; 2, 5, 6; 0, 1, 3]
new matrix would be A = [1+2,5,3; 0, 12+1, 0; 0, 0, 6]
I have been trying to create a for-loop to create this.
% code
A= zeros(3,3)
for i=1:3
for j=1:3
if x==i & y==j
A(i,j) = mass ( -- not sure how to indicate the point to get mass from)
end
end
end
Thanks!
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 7 月 7 日
This is not clear

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 7 月 7 日
A = accumarray([y(:),x(:)],mass(:))
  2 件のコメント
Elise
Elise 2015 年 7 月 7 日
Thanks, that worked out perfectly!
I notice that for this to work all numbers must be integers, is there a similar and easy way if dealing with decimals?
Andrei Bobrov
Andrei Bobrov 2015 年 7 月 7 日
x and y - must be integers

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by