フィルターのクリア

Sum of same values in a vector (MATLAB)

5 ビュー (過去 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 Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by