I have imported an array (T) with data associated to multiple reoccurring, unique numeric variables. I would like to sum each column that has the same numeric variable, However, I can't seem to figure out how to combine the sum function with a for loop. Any help is appreciated! Thank you.
In the below example, the unique variables are in column 1. I am attempting to create a new table where the unique numeric variable stays the same, and all the remaining columns are summed exclusively for that variable. The number of columns in the output table will remain the same so the sum will be in the 1st dimension. Eg.SUM(T,1) T = [2,0,0,0,0,0,1.66;2,32.96,0,0,0,0,0;1,75.61,0,0,0,0,0;1,0,0,5.94,0,0,0;1,9.83,0,0,0,0,0;1,0,0,0,42.32,0,0;1,0,5.93,0,0,0,0;1,0,0,0,0,0.92,0;1,140.32,0,0,0,0,0;1,0,0,0,70.98,0,0;5,0,0,0,7.54,0,0;5,322.43,0,0,0,0,0;5,6367.19,0,0,0,0,0;5,0,0,8.44,0,0,0];

 Réponse acceptée

Star Strider
Star Strider le 30 Oct 2017
If I understand correctly what you want to do, this works:
T = [2,0,0,0,0,0,1.66;2,32.96,0,0,0,0,0;1,75.61,0,0,0,0,0;1,0,0,5.94,0,0,0;1,9.83,0,0,0,0,0;1,0,0,0,42.32,0,0;1,0,5.93,0,0,0,0;1,0,0,0,0,0.92,0;1,140.32,0,0,0,0,0;1,0,0,0,70.98,0,0;5,0,0,0,7.54,0,0;5,322.43,0,0,0,0,0;5,6367.19,0,0,0,0,0;5,0,0,8.44,0,0,0];
Tsum = sum(T(:,2:end), 2);
SV = accumarray(T(:,1), Tsum);
Result = [(1:max(T(:,1)))' SV]
Result =
1 351.85
2 34.62
3 0
4 0
5 6705.6

4 commentaires

Kimberly Miles
Kimberly Miles le 30 Oct 2017
Thank you for the quick answer!
Your method works for separating the variables! I see now that my wording may have been unclear. The columns were not maintained as I had hoped. I would like each row to be summed (not column, i apologize), and rather have each column preserved. Is it difficult to switch the direction of the sum? I am not familiar with the coding techniques you used.
If you want sums by ‘T(:,1)’ for each column, this works:
T = [2,0,0,0,0,0,1.66;2,32.96,0,0,0,0,0;1,75.61,0,0,0,0,0;1,0,0,5.94,0,0,0;1,9.83,0,0,0,0,0;1,0,0,0,42.32,0,0;1,0,5.93,0,0,0,0;1,0,0,0,0,0.92,0;1,140.32,0,0,0,0,0;1,0,0,0,70.98,0,0;5,0,0,0,7.54,0,0;5,322.43,0,0,0,0,0;5,6367.19,0,0,0,0,0;5,0,0,8.44,0,0,0];
for k1 = 1:size(T,2)
SV(:,k1) = accumarray(T(:,1), T(:,k1));
end
Result = [(1:max(T(:,1)))' SV]
Result =
Columns 1 through 6
1 8 225.76 5.93 5.94 113.3
2 4 32.96 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0
5 20 6689.6 0 8.44 7.54
0.92 0
0 1.66
0 0
0 0
0 0
The accumarray function only wants vectors for the second argument, so looping through each column of ‘T’ was necessary. Violating that constraint produces:
Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
Since you want results, not error messages, the loop is necessary.
Kimberly Miles
Kimberly Miles le 30 Oct 2017
Thank you! That's it exactly.
Star Strider
Star Strider le 30 Oct 2017
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by