Effacer les filtres
Effacer les filtres

Sum of same values in a vector (MATLAB)

5 vues (au cours des 30 derniers jours)
high speed
high speed le 5 Jan 2022
Réponse apportée : Voss le 5 Jan 2022
Dear;
I have two vectors as follows:
V1=[3 2 3 3 2 3 2 3 3 2]
V2=[5;6;5;5;5]
I want obtain the sum of the equal values in each vector. For example in V1, I have six (3) so the sum is 18 and I have four (2) so the sum is 8.
How can I obtain this sum in MATLAB in general way

Réponse acceptée

Voss
Voss le 5 Jan 2022
V1=[3 2 3 3 2 3 2 3 3 2];
V2=[5;6;5;5;5];
uV1 = unique(V1);
for i = 1:numel(uV1)
disp(sum(V1(V1 == uV1(i))));
end
8 18
uV2 = unique(V2);
for i = 1:numel(uV2)
disp(sum(V2(V2 == uV2(i))));
end
20 6

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by