How can add the rows in n-by-m matrix that have the same indicator in the last column?

1 vue (au cours des 30 derniers jours)
Hi,
How can add the rows in n-by-m matrix that have the same indicator in the last column?
For example: 10-by-3 matrix, the third column is the indecator:
253638 3309 2
223389 3309 2
255968 3309 2
243943 33309 2
245568 63309 1
242490 93309 3
268464 23309 2
272665 53309 1
241206 83309 1
259310 13309 2
I want to add all rows (all values in first column with the same indicator) with ( all values in second column with the same indicator).

Réponse acceptée

Jos (10584)
Jos (10584) le 6 Juin 2016
X = [253638 3309 2
223389 3309 2
255968 3309 2
243943 33309 2
245568 63309 1
242490 93309 3
268464 23309 2
272665 53309 1
241206 83309 1
259310 13309 2]
OUT = cell2mat(arrayfun(@(k) [sum(X(X(:,3)==k,[1 2]),1) k], unique(X(:,3)),'un',0))
  2 commentaires
Abdulatif Alabdulatif
Abdulatif Alabdulatif le 6 Juin 2016
Thanks for a single line answer!!
Could you add another column which contains how many data points belong to each class?
Jos (10584)
Jos (10584) le 6 Juin 2016
Juts modify the function used by arrayfun
OUT = cell2mat(arrayfun(@(k) [sum(X(X(:,3)==k,[1 2]),1) k sum(X(:,3)==k)], unique(X(:,3)),'un',0))

Connectez-vous pour commenter.

Plus de réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 6 Juin 2016
Modifié(e) : Azzi Abdelmalek le 6 Juin 2016
You can use accumarray function
A=[253638 3309 3
223389 3309 2
255968 3309 2
243943 33309 2
245568 63309 1
242490 93309 3
268464 23309 2
272665 53309 1
241206 83309 1
259310 13309 2]
[ii,~,jj]=unique(A(:,3))
out=accumarray(jj,(1:numel(jj))',[],@(x) {sum(A(x,1:2),1)})
out=[ cell2mat(out) ii]
  2 commentaires
Walter Roberson
Walter Roberson le 6 Juin 2016
That would be tricky to do without one accumarray call per column.
Abdulatif Alabdulatif
Abdulatif Alabdulatif le 6 Juin 2016
The first command is work!
the next two command show the following error:
Index exceeds matrix dimensions.
Error in @(x){sum(A(x,1:2),1)}

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 6 Juin 2016
unique() to find the unique indicators and the number of indicators. Allocate an zero array of appropriate size to hold the results. Loop over the unique indicators finding the rows that match and totaling them and storing that in the results array.
accumarray() is not suitable for your purposes because accumarray cannot operator on row vectors of values.
Alternately if you convert the data into a table, you could perhaps use varfun()
  4 commentaires
Abdulatif Alabdulatif
Abdulatif Alabdulatif le 6 Juin 2016
Modifié(e) : Abdulatif Alabdulatif le 6 Juin 2016
I got the following error for the second line of the loop. The marix name is y
Index exceeds matrix dimensions.
Error in kmc (line 46)
results(K, :) = sum( y( y(:,end) == ind, 1:end-1) );
what is the issue in this case?
Abdulatif Alabdulatif
Abdulatif Alabdulatif le 6 Juin 2016
Modifié(e) : Stephen23 le 6 Juin 2016
It was my machine problem and the code works prefectly.
Thanks

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by