Can this silly accumarray for-loop be removed by a vectorization?

조회 수: 1 (최근 30일)
Peta
Peta 2015년 8월 17일
댓글: Peta 2015년 8월 18일
I have a matrix A where some of the values in column 1 are duplicates. Where a duplicate exists in column 1 (representing a timestamp) I want to replace the corresponding values in the other columns by a mean value of the duplicates.
Right now Im doing it by identifying the unique values and then loop through the columns one by one via a temporary variable since accumarray only accepts one column at a time:
[UA,~,idx] = unique(A(:,1)); % Finds duplicates in the first column of matrix A
for i = 28:-1:2
Temp = [UA,accumarray(idx,A(:,i),[],@mean)]; % Replaces duplicates with the mean of the duplicate values in column i
B(:,i) = Temp(:,2); % Transfers the results and builds the final matrix B containing the same as A but with no duplicates
end
It is doing what I want but it is really slow, is there any simple way of rewriting it so I don’t have to deal with the for-loop?

채택된 답변

Sean de Wolski
Sean de Wolski 2015년 8월 18일
편집: Sean de Wolski 2015년 8월 18일
A = randi(32280,28);
[UA,~,idx] = unique(A(:,1));
[X,Y] = ndgrid(idx, 2:size(A,2));
newcolumns = accumarray([X(:), Y(:)], reshape(A(:,2:end),[],1), [], @mean);
The ndgrid needs to start in the second column and only extract the 2nd through end columns of A for vals.
  댓글 수: 1
Peta
Peta 2015년 8월 18일
Thank you that worked as it should. But sadly it turned out to be slower compared to the original for-loop method.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Kelly Kearney
Kelly Kearney 2015년 8월 18일
I'm not sure if it will be faster, but you may try my aggregate function. It only calls accumarray once under the hood, though, using indices to make the application to multiple columns a little more efficient.
[~, B] = aggregate(A(:,1), A, @(x) mean(x,1));
B = cat(1, B{:});

Walter Roberson
Walter Roberson 2015년 8월 17일
colidx = repmat(1:size(A,2), size(A,1), 1);
newcolumns = accumarray([idx, colidx(:)], reshape(A(2:end,:),[],1), [], @mean);
output = [UA, newcolumns];
  댓글 수: 3
Walter Roberson
Walter Roberson 2015년 8월 17일
Hmmm... Try
[X,Y] = ndgrid(idx, 1:size(A,2));
newcolumns = accumarray([X(:), Y(:)], reshape(A(2:end,:),[],1), [], @mean);
Peta
Peta 2015년 8월 18일
편집: Peta 2015년 8월 18일
No, it’s still not quite right I think.
I’m getting an error saying that VAL must have one element for each row in SUBS. Which is because [X(:), Y(:)] which corresponds to SUBS has a slightly different dimension than reshape(A(2:end,:),[],1) which would be VAL.
A = randi(32280,28);
[UA,~,idx] = unique(A(:,1));
[X,Y] = ndgrid(idx, 1:size(A,2));
newcolumns = accumarray([X(:), Y(:)], reshape(A(2:end,:),[],1), [], @mean);
Error using accumarray
Second input VAL must be a vector with one element for each row in SUBS, or a scalar.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by