How can one "summarize a matrix with the first four columns"?

6 Ansichten (letzte 30 Tage)
alpedhuez
alpedhuez am 29 Jul. 2020
Kommentiert: Jon am 7 Aug. 2020
First, I have a matrix like
1 2 3 4 1.1
1 2 3 4 3
1 2 3 5 4
2 3 4 1 6
...
Second, what I would like to do is to summarize the matrix with the first four columns. That is, here, I want to take an average of the entries of the fifth item for the rows that has the same first four columns. In this example, it will be
1 2 3 4 2.05
1 2 3 5 4
2 3 4 1 6
...
Please advise.

Akzeptierte Antwort

Jon
Jon am 29 Jul. 2020
Bearbeitet: Jon am 29 Jul. 2020
Here is one way to do it. There might be some clever way to fully vectorize (eliminate the loop) this, but this will work if performance on huge arrays isn't an issue
% define your original matrix
A = [1 2 3 4 1.1;1 2 3 4 3; 1 2 3 5 4; 2 3 4 1 6]
% find rows with unique first four elements
[C,~,ic] = unique(A(:,1:4),'rows')
% summarize by finding average of elements in fifth column over rows with
% same first four columns
for k = 1:length(ia)
% use logical indexing to find rows with unique first four elements
C(k,5) = mean(A(ic==k,5))
end
  2 Kommentare
alpedhuez
alpedhuez am 29 Jul. 2020
Thank you. There might be "accumarray" solution in the third part that I would like to understand.
Jon
Jon am 7 Aug. 2020
Good idea using accumarray. I didn't know that command. Definitely looks like it has some possibilities.
C(:,5) = accumarray(ic,A(:,5))./accumarray(ic,1)
instead of the for loop works for this case. I would have to think about it more to know if there are any edge cases where this would fail, but I think it maybe a nice way to do it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by