help counting number of times a value occurs across matrix

I have an 19x100 matrix, dp, that can have a variety of values in it
111, 112, 121, 122, 211, 212, 221, 222, 311, 312, 321, 322, 411, 412, 421, 422
I want to count the number of times each value occurs in the matrix, thus, the number for each count should never be more than 100 and the sum of all the counts across each row should be 100
I've tried using the sum function but that doesn't seem to be working with the way I have it set up
count_111 = sum(dp == 111,2);
I thought this would sum up the number of times 111 occurs across each row but it's summing 111 with all the 111
Which function should I use to just count the number of times each value occurs?

4 comentarios

"I thought this would sum up the number of times 111 occurs across each row"
Yes, that's what it does:
dp = [111 112 121 111 211; 111 111 321 411 111]
dp = 2×5
111 112 121 111 211 111 111 321 411 111
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
count_111 = sum(dp == 111,2)
count_111 = 2×1
2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
111 appears 2 times in the first row of dp and 3 times in the second row of dp, exactly as indicated by count_111.
"but it's summing 111 with all the 111"
What does that mean?
Whoops I realized what the issue was. I was changing the length of the matrix (it's t x N and I was changing N around) and when I changed matrix size, it wasn't completely overriding the previous matrix. i.e. if I ran the simulation with N = 10,000 and dp was a size of 10,000 and then ran it with N = 10, the size of dp would still be 10,000. That's why it was coming out weird.
It was working correctly actually! I might need to take a break from looking at this, I think it's making my eyes cross.
That's one reason why preallocation is a good idea.
Yeah, this matrix wasn't intially meant to be created, it was made very early on to try and identify a problem, so I didn't premake it and fill it with zeros, it was just thrown into the for loop. That has now been fixed.

Iniciar sesión para comentar.

Respuestas (2)

values = [111, 112, 121, 122, 211, 212, 221, 222, 311, 312, 321, 322, 411, 412, 421, 422];
edges=[values,inf];
dp = [111 112 121 111 211; 111 111 321 411 111]
dp = 2×5
111 112 121 111 211 111 111 321 411 111
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for i=1:height(dp)
counts(i,:)=histcounts(dp(i,:),edges);
end
counts
counts = 2×16
2 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Bruno Luong
Bruno Luong el 5 de Nov. de 2024
Editada: Bruno Luong el 5 de Nov. de 2024
Alternative way of counting
% Random test array
A = round(10 + 2 * randn(3,10))
A = 3×10
10 12 11 11 9 10 8 11 14 9 12 12 12 12 11 13 12 9 8 9 10 5 11 7 11 11 9 10 11 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Count #repetitions by row
[value,~,J] = unique(A);
[m,n] = size(A);
I = repmat(1:m,1,n);
C = accumarray([I(:),J],1);
% Make nice output
Counts = num2cell(C.',1);
Varnames = sprintfc('CountRow%d', 1:m);
CountTable = table(value, Counts{:}, VariableNames = ['Value', Varnames])
CountTable = 9x4 table
Value CountRow1 CountRow2 CountRow3 _____ _________ _________ _________ 5 0 0 1 7 0 0 1 8 1 1 0 9 2 2 2 10 2 0 2 11 3 1 4 12 1 5 0 13 0 1 0 14 1 0 0

Categorías

Etiquetas

Preguntada:

el 4 de Nov. de 2024

Editada:

el 5 de Nov. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by