Digits that repeat in an n by m matrix

조회 수: 1 (최근 30일)
PEDRO ALEXANDRE Fernandes
PEDRO ALEXANDRE Fernandes 2022년 3월 5일
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
Matt J
Matt J 2022년 3월 5일
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 2022년 3월 5일
True. Sorry..

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

KSSV
KSSV 2022년 3월 5일
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 2022년 3월 5일
편집: Matt J 2022년 3월 5일
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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by