Count how many times a number is repeated in a certain row of an array

Please consider the array
A = [1;1;1;2;2;2;2;2;3;3;4;4;4;4;4;4;4;5;5;5;5];
I would like to determine how many times each number repeats.
For 1, it repeats three times. For 2, it repeats five times, and so on.
If there is other data in columns to the left of the array A that does not follow the same repeating pattern, can I still count how many times each number in a certain column is repeated?

2 Comments

doc histcounts
would be one way of doing it, depending on how many unique values you are likely to have, though however many there are you could use that many bins.
am wondering as well

Sign in to comment.

 Accepted Answer

Here is a code;
A = [1;1;1;2;2;2;2;2;3;3;4;4;4;4;4;4;4;5;5;5;5];
c = unique(A); % the unique values in the A (1,2,3,4,5)
for i = 1:length(c)
counts(i,1) = sum(A==c(i)); % number of times each unique value is repeated
end
% c(1) is repated count(1) times

16 Comments

The code can be adjusted to check a particular column of A by simply changing A to A(:,n)
Sure what I mean is if
A=[randi([1,10],length(A),3),A];
I use three columns of random integers and concatenate them to the left of A so that there are columns that do not repeat what is in the last column of A. Does that make sense?
Assuming you are interested in only the 4th column repetitions;
A = [1;1;1;2;2;2;2;2;3;3;4;4;4;4;4;4;4;5;5;5;5];
A=[randi([1,10],length(A),3),A];
c = unique(A(:,4));
for i = 1:length(c)
counts(i,1) = sum(A(:,4)==c(i));
end
[c counts]
ans =
1 3
2 5
3 2
4 7
5 4
1 is repeated 3 times, 2 is repeated 5 times, 3 is repeated 2 times ...
Is this what you are trying to achieve?
Show an example of the output you want.
A = [1 2 3 3 3; 4 4 4 5 6]
DesiredOutput = ???
The answer from Aquatris is what I am looking for.
Hi again.
Is there a way to take the code that you wrote and apply the section
sum(A(:,4)==c(i))
so that it checks entire rows? For instance, instead of having A as I wrote it above, what if A was
A = [1 1 1; 1 1 1; 2 2 2; 3 3 3; 3 3 3; 3 3 3];
I would like to get a final result
[c counts]
ans =
1, 1, 1 2
2, 2, 2 1
3, 3, 3 3
Thanks!
Is this still unsolved? You accepted this answer and said Aquatris's answer does what you want, though I'm not sure since it doesn't do what you originally asked for. A is a 21 row vector - each column has only one number so the count for each row is simply 1. However your original explanation seems to want a histogram along columns, not rows like the subject line said. Then you changed A to a matrix but each row has only one number, so the counts would now be 3 for every row, yet you don't have that as your desired answer. So it's very confusing and ambiguous. But since you've accepted an answer, I'm assuming all is solved, so I won't worry about it further.
Yes, it was solved, the problem is later I realized that I should be counting repeated rows across the first three columns instead. Aquatris's answer works exactly for what I thought I needed before.
Nevermind, someone already asked how to do this.
https://www.mathworks.com/matlabcentral/answers/282561-count-numbers-of-identical-rows
I just had to find the unique rows of the final result and I have what I need.
Glad you found the answer you were looking for :)
Me too! I've got people waiting patiently on results.
And thank you for all the thought you put into your answer. I actually did use it.
error is showing : counts(i,1) = sum(A==c(i));
Hi, is there any way you know which would allow me to get the values in the vector that appear say less than a certain amount of times?
Thanks.
From the above example, the code can be (for relatively newer versions of Matlab;
d = c(counts<4);
where d has the value that appear less than 4 times in the A vector. Alternatively;
I = find(counts<4); % find indices where counts variable elements are less than 4
d=c(I);
Hi. I have a similar problem. if i have a multidimensional array of the style 7 X 10
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
and I intend to count the number of times each digit appears per line.
Example: on line 1
1 -> 1
2 -> 3
3 -> 2
..
and then the same in the following lines. Any idea?

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2016a

Tags

Asked:

on 20 Jul 2018

Moved:

DGM
on 20 Jul 2024

Community Treasure Hunt

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

Start Hunting!