Группировка и подведение итогов в Matlab - PullRequest
1 голос
/ 28 марта 2012

У меня есть простая матрица с повторяющимися значениями в некоторых столбцах. Мне нужно сгруппировать данные по именам и неделям и суммировать цены, потраченные на каждый день данной недели. вот пример:

 name day  week  price
 John 12   12    200
 John 14   12    70
 John 25   13    150
 John 1    14    10
 Ann  13   12    100
 Ann  15   12    100
 Ann  20   13    50

Желаемый результат будет:

  name week sum
  John 12   270
  John 13   150
  John 14   10
  Ann  12   200
  Ann  13   50

Есть ли хороший способ сделать это? Я использовал для циклов, но не уверен, что это лучший способ сделать это:

names= unique(data(:,1)); % getting unique names from data
n=size(names, 1);         % number of unique names
m=size(data(:,1),1);      % number of total rows
sum=[];                   % empty matrix for writing the results
for i = 1:n             
        temp=[];          % creating temporar matrix  
        k=1;
    for j=1:m
        if name(i)==data(j,1)     % going through all the rows and getting the rows of 
            temp(k,:)=data(j,:);  % the same name and putting in temporar matrix
            k=k+1;
        end
    end
    count=0;
    s=1;
    for l = 1:size(temp,1)-1      % going through temporar matrix of one name(e.g.John)
        if temp(l,3)==temp(l+1,3) % checking if the day of current row is equal to the
         count=count+temp(l,4);   % date of the next row (the data is sorted by name 
        else                      % and date) and then summing the prices 4th column
            sum(s, 1:3)=[names(i) temp(l,3) count];  
            count=0;              % if the days are not equal, then writing the answer
            s=s+1;                % to the output matrix sum
        end        
    end 
end  

Ответы [ 2 ]

3 голосов
/ 29 марта 2012

Используйте accumarray.Он будет группировать и агрегировать значения, подобные этому.Вы можете использовать третий аргумент otuput из unique(data(:,1)), чтобы получить числовые индексы для передачи в subs аргумент accumarray.Подробнее см. doc accumarray.

1 голос
/ 29 марта 2012

Вероятно, самый простой способ - использовать функцию GRPSTATS из Statistical Toolbox.Сначала вы должны объединить name и week для создания групп:

[name_week priceSum] = grpstats(price, strcat(name(:), '@', week(:)), {'gname','sum'});
...