Counting occurrences of each column in a matrix

21 ビュー (過去 30 日間)
Daniel Ring
Daniel Ring 2018 年 10 月 8 日
コメント済み: Daniel Ring 2018 年 10 月 8 日
I have a matrix with 16 columns and a very large number of rows. Each column has two possible outcomes in it (for example 1 or 0 in one column, .6 and .4 in another). I would like to count each of these occurrences in the columns. Preferably, I'd like to have a vector that counts each outcome of its respective column. Thank you!
[EDITED, Jan, moved from section for answers]
Example: A = [1 .6 .7 .8; 0 .4 .3 .2; 1 .6 .7 .8; 1 .6 .3 .8]
ans = [3 3 2 2 ; 1 1 2 2]
  3 件のコメント
Jan
Jan 2018 年 10 月 8 日
The example is not clear: 0.8 occurs 3 times, so shouldn't the last value of the output be 3 or perhaps 1? Why is the first column of the output [3;1] and not [1;3]?
Daniel Ring
Daniel Ring 2018 年 10 月 8 日
I'm sorry, it should be ans = [3 3 2 3 ; 1 1 2 1] The first column in [3;1] because 1 occurs 3 times and 0 occurs 1 time.

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

採用された回答

ANKUR KUMAR
ANKUR KUMAR 2018 年 10 月 8 日
Since you have not given any sample data, I am taking some random data.
A=randi(50,50,50);
A(A<=25)=0.4;
A(A>25)=0.6;
A contains only 0.4 and 0.6 only, spreaded out completely. The below program gives you the occurrence of 0.4 and 0.6 in every coloumn.
nums1=arrayfun(@(x) length(find(A(:,x)==0.4)),1:size(A,2));
nums2=arrayfun(@(x) length(find(A(:,x)==0.6)),1:size(A,2));
occur=[nums1' nums2'];
nums2 can also be calculated as
nums2=size(A,1)-nums1;

その他の回答 (2 件)

Jan
Jan 2018 年 10 月 8 日
編集済み: Jan 2018 年 10 月 8 日
Maybe:
A = [1 .6 .7 .8; ...
0 .4 .3 .2; ...
1 .6 .7 .8; ...
1 .6 .3 .8];
Count = sum(A == A(1, :), 1); % Auto-expand: >= R2016b
Result = [Count; size(A, 1) - Count];
For older Matlab versions:
Count = sum(bsxfun(@eq, A, A(1, :)), 1);

dpb
dpb 2018 年 10 月 8 日
Several ways to do this; probably easiest is via converting to categorical with the unique values in each column transformed to a single pair of categories--[0|1], [Y|N], [HI|LO], ..., whatever makes sense for the meanings.
Then histogram on those variables to return counts.
Or, use unique and the returned (optional) second index array to bin over.

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by