MATLAB: Count number of times value appears in column

beginnercountfor loopmatrix

I have a 1000×296 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 296×296 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!

Best Answer

% 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