Summing over an arbitrary Dimension in a Coordinate list

I have a multidimensional array stored as a coordinate list in a n by 5 matrix. The first four columns are x,y,z,and energy coordinates respectively and the fifth column is the tally result. I want to be able to sum over an arbitrary dimension. The code I have written so far can only sum over the final dimension to iterate.
repetitionFactor = find(diff(data(:,4)),1);
summed = [data(1:repetitionFactor,1:4) sum(reshape(data(:,5),repetitionFactor,[])')']
summed(:,4)=inf;
Which on the following matrix
[1,1,1,1,1;1,2,1,1,2;2,1,1,1,3;2,2,1,1,4;1,1,2,1,5;1,2,2,1,6;2,1,2,1,7;2,2,2,1,8;1,1,1,2,9;1,2,1,2,10;2,1,1,2,11;2,2,1,2,12;1,1,2,2,13;1,2,2,2,14;2,1,2,2,15;2,2,2,2,16;1,1,1,3,17;1,2,1,3,18;2,1,1,3,19;2,2,1,3,20;1,1,2,3,21;1,2,2,3,22;2,1,2,3,23;2,2,2,3,24]
Gives the expected result of
[1,1,1,Inf,27;1,2,1,Inf,30;2,1,1,Inf,33;2,2,1,Inf,36;1,1,2,Inf,39;1,2,2,Inf,42;2,1,2,Inf,45;2,2,2,Inf,48]
I would like to be able to generate a similar result for any dimension like for z. Which should be
[1,1,Inf,1,6;1,2,Inf,1,8;2,1,Inf,1,10;2,2,Inf,1,12;1,1,Inf,2,22;1,2,Inf,2,24;2,1,Inf,2,26;2,2,Inf,2,28;1,1,Inf,3,38;1,2,Inf,3,40;2,1,Inf,3,42;2,2,Inf,3,44]

2 Comments

I just realized I was too focused on leaving the original order intact when it didn't matter. By presorting the matrix with sortrows() I could just use the code I had so far.
So the final code is:
columnNumber = 3;
data = sortrows(data,columnNumber);
repetitionFactor = find(diff(data(:,columnNumber)),1);
summed = [data(1:repetitionFactor,1:4) sum(reshape(data(:,5),repetitionFactor,[])')'];
summed(:,columnNumber)=inf;
I'm not sure how you have multi dimensions. Sounds like only two to me. You have "a n by 5 matrix." which is 2-D. But it's not clear - sounds like you have solved your own problem. If not, let us know.

Sign in to comment.

Answers (0)

Categories

Asked:

on 19 Feb 2017

Commented:

on 19 Feb 2017

Community Treasure Hunt

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

Start Hunting!