按特定索引值的数组总和

44次观看(最近30天)
Khairul Nur
Khairul Nur 2021年5月29日
编辑: KALYAN ACHARJYA 2021年5月29日
嗨,我有两个阵列。一个包含索引和第二个数组(Hasil_tambah_array)的第一个数组,其中包含以下值的值:
索引=
4
2
4
hasil_tambah_array =
3.6881
0.8506
0.0132
0.8506
10.5147
12.5147
14.5074
21.8003
11.2145
18.5074
I try to sum the second array as per index such as 3.6881,0.8506,0.0132 and 0.8506 (since the 1st index is 4) will be sum together . Then 10.5147 and 12.5147 (since the 2nd index is 2) will be sum up and respectively (next will be the rest since the index is 4).
感谢您的时间解决这个问题。TQIA。

接受的答案

DGM
DGM 2021年5月29日
编辑:DGM 2021年5月29日
这是一种方式。这可能不是最有效的方法,但它可以完成工作。
IDX = [4 2 4];
h = [3.6881 0.8506 0.0132 0.8506 10.5147 12.5147 14.5074 21.8003 11.2145 18.5074]。
s = cellfun(@sum,mat2cell(h,idx,1))
s = 3×1
5.4025 23.0294 66.0296
记住,使用mat2cell细分th ()e main vector requires that sum(idx) = numel(H), so watch out for that.

更多答案(1)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021年5月29日
编辑:KALYAN ACHARJYA 2021年5月29日
结果= sum(second_arary(first_array))
更多的
结果= sum(hasil_tambah_array(index))
2条评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2021年5月29日
索引= [4
2
4];
hasil_tambah_array = [3.6881
0.8506
0.0132
0.8506
10.5147
12.5147
14.5074
21.8003
11.2145
18.5074];
结果= sum(hasil_tambah_array(index))
输出:
结果=
2.5518
更多:与
>> 0.8506+0.8506+0.8506
ans =
2.5518

登录发表评论。

社区寻宝

在Matlab Central中找到宝藏,发现社区如何为您提供帮助!

开始狩猎!