Count number of times value appears in column

9 ビュー (過去 30 日間)
Emma Kuttler
Emma Kuttler 2019 年 11 月 22 日
編集済み: Image Analyst 2019 年 11 月 23 日
I have a 1000x296 matrix called FinalRanking. The possible values in each cell are between 1 and 296, integers.
I want to count the number of times each number, 1-296, appears in each column, and return this as a 296x296 matrix, lets call it counter.
so for example Counter(1,1) has the value of the number of time 1 appears in Column 1 of FinalRanking.
or Counter(12, 57) returns the value of the number of times 12 appears in Column 57 of FinalRanking.
How would I do this? I'm assuming i need nested for loops. Thanks!

採用された回答

the cyclist
the cyclist 2019 年 11 月 22 日
編集済み: the cyclist 2019 年 11 月 22 日
% Parameterize the max value, for convenience
V = 296;
% Some simulated data
FinalRanking = randi(V,1000,V);
% Preallocate the counter array
counter = nan(V,V);
% Loop over the columns, and count the number of times each value appears
for nc = 1:V
counter(:,nc) = histcounts(FinalRanking(:,nc),[1:V Inf]);
end
  1 件のコメント
Emma Kuttler
Emma Kuttler 2019 年 11 月 22 日
This works perfectly, thank you!

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

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2019 年 11 月 22 日
編集済み: Andrei Bobrov 2019 年 11 月 22 日
out = histc(FinalRanking,1:296);
  4 件のコメント
Andrei Bobrov
Andrei Bobrov 2019 年 11 月 22 日
"If performance is a problem due to a large number of columns in the matrix, then consider continuing to use histc for the column-wise bin counts." - wrote The Mathworks.
the cyclist
the cyclist 2019 年 11 月 23 日
Make up your mind, MathWorks! :-)

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


Image Analyst
Image Analyst 2019 年 11 月 22 日
Try this:
edges = 1 : 297;
FinalRanking = randi(296, 1000, 296);
counter = zeros(296, 296);
for col = 1 : size(FinalRanking, 2)
counter(:, col) = histcounts(FinalRanking(:, col), edges);
end
imshow(counter, [], 'ColorMap', hsv(256));
axis('on', 'image');
colorbar;
impixelinfo;
  2 件のコメント
Emma Kuttler
Emma Kuttler 2019 年 11 月 23 日
This is interesting! I'm guessing the x axis represents the column and y represents the number of occurences? If so, how do i modify the code to add axis labels?
Image Analyst
Image Analyst 2019 年 11 月 23 日
編集済み: Image Analyst 2019 年 11 月 23 日
Yes for x. But along the y axis are the histogram bins. The value, "z" value if you want to think of it like that, is the count. But the actual distance/location along the y axis is the bin number, not the "number of occurrences" (counts).
fontSize = 20; % Whatever.
xlabel('Column', 'FontSize', fontSize);
ylabel('Number (i.e., histogram bin)', 'FontSize', fontSize);

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by