Sum of same values in a vector (MATLAB)

18 Ansichten (letzte 30 Tage)
high speed
high speed am 5 Jan. 2022
Beantwortet: Voss am 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

Akzeptierte Antwort

Voss
Voss am 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

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by