Sum of same values in a vector (MATLAB)

7 次查看(过去 30 天)
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

采纳的回答

Voss
Voss 2022-1-5
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by