Filter löschen
Filter löschen

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

122 Ansichten (letzte 30 Tage)
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 Kommentare
Adam
Adam am 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.
NSHh
NSHh am 15 Mai 2021
Verschoben: DGM am 20 Jul. 2024 um 18:18
am wondering as well

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Aquatris
Aquatris am 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 Kommentare
Aquatris
Aquatris am 5 Mai 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
PEDRO ALEXANDRE Fernandes am 4 Mär. 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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by