Finding the repetition of each element of a matrix
Show older comments
I have a matrix named A with size N×3. For example,
A = [ 1 2 3;
1 4 5;
5 6 7;
1 3 4;
5 6 7;
1 4 7;
4 3 2;
7 8 9]
In this example, The elements of A are:
B = [1 2 3 4 5 6 7 8 9]
How can I find the repetition of each element of B in A? Actually I am looking for a a vector array like C:
C = [4 2 3 4 3 2 4 1 1]
Here is what I did:
for i=1:numel(B)
C(i) = numel(find(ismember(A, i)==1));
end
I would appreciate it if someone could help me how I can find the repetition of each element of A in a better way and avoid the for-loop.
Accepted Answer
More Answers (1)
Image Analyst
on 21 May 2014
Try this:
edges = min(A(:)) : max(A(:))
counts = histc(A(:), edges)
Categories
Find more on Logical 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!