フィルターのクリア

How to find the frequency of each row with certain coordiante in a matrix?

3 ビュー (過去 30 日間)
Mnr
Mnr 2014 年 3 月 29 日
回答済み: Aditya Rathore 2022 年 2 月 1 日
Hello all,
I have an mx2 matrix with its rows (0,1), (-1,0), (0,1), (0,-1), (1,1), (-1,1), (1,-1),(-1,-1); I would like to find the frequency of each of above coordinates. In other words, if I have A=[1 1;0 1;-1 1;1 0;-1 1], I would like to get something like,
number of times that (1,1) has appeared=1; number of times that (0,1) has appeared=1; number of times that (-1,1) has appeared=2; number of times that (1,0) has appeared=1; number of times that (0,-1) has appeared=0; number of times that (-1,-1) has appeared=0; number of times that (-1,0) has appeared=0; number of times that (1,-1) has appeared=0;
when I use "find" command I get an error. Thank you.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 3 月 29 日
編集済み: Azzi Abdelmalek 2014 年 3 月 29 日
A=[1 1; 0 1; 1 0; -1 1;-1 1;0 1;0 1]
[ii,jj,kk]=unique(A,'rows','stable')
f=histc(kk,1:numel(jj)); % Frequency
result=[ii f]
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 3 月 29 日
編集済み: Azzi Abdelmalek 2014 年 3 月 29 日
Ok use this (without stable option), maybe you have an old version of Matlab
A=[1 1; 0 1; 1 0; -1 1;-1 1;0 1;0 1]
[ii,jj,kk]=unique(A,'rows')
f=histc(kk,1:numel(jj)); % Frequency
result=[ii f]
Mnr
Mnr 2014 年 3 月 29 日
Thank you so much! I really appreciate your help!

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

その他の回答 (1 件)

Aditya Rathore
Aditya Rathore 2022 年 2 月 1 日
There are two things:
  1. Your row length is small (2)
  2. You want to also retrieve the stored values
I suggest you make a matrix for storing frequencies
freqs = zeros(m, 2);
[rows, ~] = size(A);
for idx=1:rows
i = A(idx, 1);
j = A(idx, 2);
freqs(i+2,j+2) = freqs(i+2, j+2) + 1; %Because you have -1 also
end
When retrieving a value, simply do
count = A(i+2, j+2);
This approach is useful as you can normalize the counts and get probability using
freqs = freqs / sum( freqs, 'all');
I tried it for image based task in RGB space and found this to be fastest solution.

カテゴリ

Help Center および File ExchangeMatrix and Vector Construction についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by