Clear Filters
Clear Filters

Digits that repeat in an n by m matrix

1 view (last 30 days)
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 Comments
Matt J
Matt J on 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.

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (2)

KSSV
KSSV on 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 on 5 Mar 2022
Edited: Matt J on 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

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!