Count how many times a number is repeated in a certain row of an array
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
Adam
on 20 Jul 2018
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
Accepted Answer
Aquatris
on 20 Jul 2018
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
Aquatris
on 20 Jul 2018
The code can be adjusted to check a particular column of A by simply changing A to A(:,n)
Nathaniel Werner
on 20 Jul 2018
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?
OCDER
on 20 Jul 2018
Show an example of the output you want.
A = [1 2 3 3 3; 4 4 4 5 6]
DesiredOutput = ???
Nathaniel Werner
on 20 Jul 2018
The answer from Aquatris is what I am looking for.
Nathaniel Werner
on 20 Jul 2018
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!
Image Analyst
on 20 Jul 2018
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.
Nathaniel Werner
on 20 Jul 2018
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.
Nathaniel Werner
on 20 Jul 2018
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.
Aquatris
on 20 Jul 2018
Glad you found the answer you were looking for :)
Nathaniel Werner
on 20 Jul 2018
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.
Thet Win
on 23 Jun 2019
error is showing : counts(i,1) = sum(A==c(i));
Adam
on 24 Jun 2019
works fine for me.
Dibyajyoti Das
on 5 May 2020
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.
Aquatris
on 5 May 2020
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);
PEDRO ALEXANDRE Fernandes
on 4 Mar 2022
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?
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)