Sum of same values in a vector (MATLAB)

조회 수: 17 (최근 30일)
high speed
high speed 2022년 1월 5일
답변: Voss 2022년 1월 5일
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