MATLAB Answers

Naime
0

find how many times same element is repeated

Asked by Naime
on 4 Apr 2019
Latest activity Commented on by madhan ravi
on 4 Apr 2019
A=[9,8;7,6;1,2;2,4;3,4;3,4;4,7;6,7;8,6;9,8;7,6];
a = sort(unique(A,'rows'),2);
a is [1,2;2,4;3,4;4,7;6,7;6,7;6,8;8,9];
I want to get how many times same element is repeated,
[1,2] repeated 1,[2,4] repeated 1, [3,4] repeated 2, [1,2] repeated 1, [4,7] repeated 1, [6,7] repeated 2

  1 Comment

Note you have a mistake in your desired result 6,7 has repeated once and 7,6 has repeated twice.

Sign in to comment.

1 Answer

Answer by madhan ravi
on 4 Apr 2019
Edited by madhan ravi
on 4 Apr 2019
 Accepted Answer

A=[9,8;7,6;1,2;2,4;3,4;3,4;4,7;6,7;8,6;9,8;7,6];
a = unique(A,'rows');
R = zeros(size(a,1),1);
for k = 1:size(a,1)
R(k) = nnz(ismember(A,a(k,:),'rows'));
end
Result = table(a,R,'VariableNames',{'Rows','Times'})
Gives:
Result =
8×2 table
Rows Times
______ _____
1 2 1
2 4 1
3 4 2
4 7 1
6 7 1
7 6 2
8 6 1
9 8 2
>>

  0 Comments

Sign in to comment.