How to count sum for values corresponding to repeated numbers in matrixes.
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
KSSV
2021년 2월 23일
편집: KSSV
2021년 2월 23일
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
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!