フィルターのクリア

How can turn two indexing vectors into a corresponding matrix that counts how many locations that point has been hit?

1 回表示 (過去 30 日間)
Basically if I have two corriponding vectors, x and y say,
x = [1;1;2;1];
y = [1;1;1;2];
I want to return a matrix that represents how many times that point was referenced in the indexing matrix. For our example it would be,
2 1
1 0
Because there are 2 (1, 1) in the indexing vectors so a 2 is displayed in the matrix and because (2,2) was never in the vectors, it displays a zero
I've tried
matrix = zeros(2, 2);
matrix(x, y) = matrix(x, y) + 1
but it returns
1 1
1 1
How can I turn two vectors like these into a matrix that shows me how many times the combomation of x and y shows up in the vectors.

採用された回答

BobH
BobH 2020 年 3 月 4 日
The diagonal of a matrix is those elements whose subscripts are (1,1), (2,2), etc
s = sparse(x, y, 1)
s =
(1,1) 2
(2,1) 1
(1,2) 1
full(s)
ans =
2 1
1 0
d = full(diag(s))
d =
2
0
  1 件のコメント
Steve Le
Steve Le 2020 年 3 月 4 日
Thank you so much! This worked perfectly; I was trying to do stuff with scatter too but this was much simpler.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 3 月 4 日
See the histcounts2 function.

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by