Effacer les filtres
Effacer les filtres

Digits that repeat in an n by m matrix

1 vue (au cours des 30 derniers jours)
PEDRO ALEXANDRE Fernandes
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 commentaires
Matt J
Matt J le 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.
PEDRO ALEXANDRE Fernandes
True. Sorry..

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 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 commentaire
PEDRO ALEXANDRE Fernandes
Fantastic. Works to perfection. I had already thought of this solution but I was missing something. Thank you very much

Connectez-vous pour commenter.

Plus de réponses (2)

KSSV
KSSV le 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

Matt J
Matt J le 5 Mar 2022
Modifié(e) : Matt J le 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

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by