How to calculate the sum of values in column 2 if column 1 has a certain value?
이전 댓글 표시
I have to analyze stock accounting. I have a large matrix (10000 x 2). In the first column I have the product code and in the second column I have the quantity taken from the storage. Every action has it's own row. How can I calculate the sum for every product?
I don't know how many product codes there are.
I have tried eg. arrayfun and to generate matrices with for loops and then the sum of the splitted matrices, but the results aren't right.
With this code I got the sum separately, but then I got stuck..
n = length(C)-1;
for i = 1:1:n
if C(i,1)~= C(i+1,1)
S = sum(B(i))
end
end
Thank you in advance!
답변 (1개)
Andrei Bobrov
2013년 9월 9일
편집: Andrei Bobrov
2013년 9월 9일
a - your array < 10000 x 2 double >
[a1,ii,ii] = unique(a(:,1));
out = [a1,accumarray(ii,a(:,2))];
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!