Borrar filtros
Borrar filtros

Digits that repeat in an n by m matrix

1 visualización (últimos 30 días)
PEDRO ALEXANDRE Fernandes
PEDRO ALEXANDRE Fernandes el 5 de Mzo. de 2022
Comentada: PEDRO ALEXANDRE Fernandes el 5 de Mzo. de 2022
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 comentarios
Matt J
Matt J el 5 de Mzo. de 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
PEDRO ALEXANDRE Fernandes el 5 de Mzo. de 2022
True. Sorry..

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (2)

KSSV
KSSV el 5 de Mzo. de 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 el 5 de Mzo. de 2022
Editada: Matt J el 5 de Mzo. de 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

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by