# Digits that repeat in an n by m matrix

2 visualizzazioni (ultimi 30 giorni)
If I have a multidimensional array of n by m and I want to count the number of digits that repeat in each row of the array.
Ex:
Columns 1 through 7
9 5 2 1 5 3 3
1 8 3 1 7 4 3
1 3 1 1 8 6 4
1 3 1 6 4 2 1
2 1 6 3 2 2 1
2 3 1 1 7 5 4
1 1 4 2 1 8 6
Columns 8 through 10
2 2 5
2 2 6
4 2 4
2 9 7
1 9 1
2 1 5
5 4 5
in the end i want:
first row i have : number 1 -> 1; 2-> 3; 3 -> 2,...
I try a few, but i don´t have any sucess..
someone can help me?
##### 2 CommentiMostra NessunoNascondi Nessuno
Matt J il 5 Mar 2022
in the end i want:
first row i have : number 1 -> 1; 2-> 3; 3 -> 2,...
That is not the number of digits that repeat in the first row (which would be 3). That is the number of repetitions of each digit.
True. Sorry..

Accedi per commentare.

### Risposta accettata

Matt J il 5 Mar 2022
A = [ 9 5 2 1 5 3 3 2 2 5
1 8 3 1 7 4 3 2 2 6
1 3 1 1 8 6 4 4 2 4
1 3 1 6 4 2 1 2 9 7
2 1 6 3 2 2 1 1 9 1
2 3 1 1 7 5 4 2 1 5
1 1 4 2 1 8 6 5 4 5];
counts=histc(A',1:max(A(:))+1)'
counts = 7×10
1 3 2 0 3 0 0 0 1 0 2 2 2 1 0 1 1 1 0 0 3 1 1 3 0 1 0 1 0 0 3 2 1 1 0 1 1 0 1 0 4 3 1 0 0 1 0 0 1 0 3 2 1 1 2 0 1 0 0 0 3 1 0 2 2 1 0 1 0 0
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Fantastic. Works to perfection. I had already thought of this solution but I was missing something. Thank you very much

Accedi per commentare.

### Più risposte (2)

KSSV il 5 Mar 2022
A = [ 9 5 2 1 5 3 3];
x = unique(A);
N = numel(x);
count = zeros(N,1);
for k = 1:N
count(k) = sum(A==x(k));
end
disp([ x(:) count ]);
1 1 2 1 3 2 5 2 9 1
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Matt J il 5 Mar 2022
Modificato: Matt J il 5 Mar 2022
A = [ 9 5 2 1 5 3 3 2 2 5
1 8 3 1 7 4 3 2 2 6
1 3 1 1 8 6 4 4 2 4
1 3 1 6 4 2 1 2 9 7
2 1 6 3 2 2 1 1 9 1
2 3 1 1 7 5 4 2 1 5
1 1 4 2 1 8 6 5 4 5];
B=A==reshape(0:9,1,1,[]);
numRepetitions=sum(sum(B,2)>1,3)
numRepetitions = 7×1
3 3 2 2 2 3 3
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su MATLAB in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by