How to count sum for values corresponding to repeated numbers in matrixes.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Georgios Tsiledakis
le 23 Fév 2021
Réponse apportée : Georgios Tsiledakis
le 26 Fév 2021
Dear experts,
I have a 2 column ascii file with numbers as it looks at the first 2 columns from the left.
Shortly:
EventID Hits Sum
----------------------------
9170 1 1
9443 2 14
9443 4 14
9443 8 14
15872 4 12
15872 8 12
16077 4 12
16077 8 12
22374 1 1
23801 4 12
How I could get the 3rd column as a product? For example, the number 9443 is repeated 3 times and I would like to place the sum 2+4+8=14 on the place fo 9443 etc
I tried by counting unique numbers like:
fid = fopen('200Mevents_file4_geant.txt', 'r');
data = fscanf(fid, '%f');
nRows = data(1);
data = reshape(data(1:end), 106695, 1).';
c = unique(data);
for i = 1:length(data)
counts(i,1) = sum(data==data(i)); % number of times each unique value is repeated
end
Could you help me?
Georgios
0 commentaires
Réponse acceptée
KSSV
le 23 Fév 2021
Modifié(e) : KSSV
le 23 Fév 2021
clc; clear all ;
data = [9170 1 1
9443 2 14
9443 4 14
9443 8 14
15872 4 12
15872 8 12
16077 4 12
16077 8 12
22374 1 1
23801 4 12] ;
[c,ia,ib] = unique(data(:,1)) ;
n = length(c) ;
iwant = zeros(n,3) ;
for i = 1:n
iwant(i,:) = [c(i) sum(data(ib==i,2)) data(ia(i),3)] ;
end
3 commentaires
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!