Borrar filtros
Borrar filtros

Sum values corresponding to a number in a vector

1 visualización (últimos 30 días)
Sourangsu Chowdhury
Sourangsu Chowdhury el 24 de Sept. de 2018
Editada: Stephen23 el 24 de Sept. de 2018
4 1
4 10
4 2
8 20
5 12
5 4
5 8
5 6
Suppose I have two vectors, I need average of the digits in second vector corresponding to the digits in the first vector such that the result is
4 13
8 20
5 30

Respuesta aceptada

Stephen23
Stephen23 el 24 de Sept. de 2018
Editada: Stephen23 el 24 de Sept. de 2018
>> M = [4,1;4,10;4,2;8,20;5,12;5,4;5,8;5,6]
M =
4 1
4 10
4 2
8 20
5 12
5 4
5 8
5 6
>> [U,~,X] = unique(M(:,1));
>> S = accumarray(X,M(:,2),[],@sum);
>> [U,S]
ans =
4 13
5 30
8 20

Más respuestas (0)

Categorías

Más información sobre NaNs en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by