How to count sum for values corresponding to repeated numbers in matrixes.
2 views (last 30 days)
Show older comments
Georgios Tsiledakis
on 23 Feb 2021
Answered: Georgios Tsiledakis
on 26 Feb 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 Comments
Accepted Answer
KSSV
on 23 Feb 2021
Edited: KSSV
on 23 Feb 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 Comments
More Answers (1)
See Also
Categories
Find more on Matrices and Arrays 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!