sum column based on match in another column

Hello, I have a 2D array of data that is imported from Excel. The data is in the following format:
Data=[Date Shift Unit Code UpOn UpOff Down]
I have already sorted the data so that it is in date sequential order. But I have 0,1 or 2 entries for each date (using matlab's serial date format), Shift can be either a 1 or a 2. I need to sum the UpOff column for each date.
I would like to use a non-looping sort to generate this sum, something along the lines of:
DataSum = Data(Data(:,3)==1 & Data(:,2)==2,6) + Data(Data(:,3)==1 & Data(:,2)==1,6)
But the data may or may not have multiple entries so this format doesn't always generate matrices with the same number of rows for each side the summation. Is there another way that I can do this without resorting to using a loop and a test for each?

 Accepted Answer

[u,~,ib]=unique(Data(:,1)); % get the unique values and locations for each
sx(:,i)=accumarray(ib,Data(:,6)); % sum over the subsets

More Answers (0)

Categories

Asked:

on 20 Nov 2015

Answered:

dpb
on 20 Nov 2015

Community Treasure Hunt

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

Start Hunting!