how do I count the number of identical rows in a matrix?

2 ビュー (過去 30 日間)
Jeremie
Jeremie 2012 年 10 月 25 日
コメント済み: reetu hooda 2018 年 5 月 24 日
Hello,
I'm trying understand how to apply the huffman coding function to a matrix of the type:
0 22
1 12
0 4
1 1
...etc
which is the result of run length encoding on a bi level image. I need to obtain a vector with all the possible characters, an another with the corresponding probabilities for the huffman coding function to accept.
more simply put, I'm trying to find a way to find all the identical rows in this matrix, divide it by the total number of rows, and write the result in a vector.
Thanks very much for your help, and let me know if I didn't follow the appropriate formatting rules or if this question has been covered before - a search did not give any results.
  1 件のコメント
Matt J
Matt J 2012 年 10 月 25 日
Your example doesn't have any identical rows in it. Show an example that does and what the output should be.

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

回答 (1 件)

Kye Taylor
Kye Taylor 2012 年 10 月 25 日
Let the N-by-2 matrix you're working with be denoted A.
Then, you can get a vector of size M-by-2 containing the unique rows with the command
uRows = unique(A,'rows');
Compute your empirical probability of each unique row with the commands
a = mat2cell(uRows,ones(1,size(uRows,1)),2); % convert to cell for cellfun
% function handle that takes a 1x2 row vector as input
% and returns the number of matching rows in A.
g = @(row)nnz(ismember(A,row,'rows'));
% the probability
puRows = cellfun(g,a)/size(A,1);
  1 件のコメント
reetu hooda
reetu hooda 2018 年 5 月 24 日
Thank you so much! exactly what I wanted.

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

カテゴリ

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