ho to find index at which the 50% of the sum vector is included?

2 ビュー (過去 30 日間)
benghenia aek
benghenia aek 2021 年 11 月 26 日
回答済み: Image Analyst 2021 年 11 月 27 日
ho to find index at which the 50% of the sum vector is included?
a=[3 5 7 3 5 8 9 3 1 41 6];
sum(a)=91
50% of sum signal =45.5
indice which is approximately equal to of sum of 50% of sum signal a is 9

回答 (2 件)

Star Strider
Star Strider 2021 年 11 月 26 日
A least-squared-difference approach works —
a=[3 5 7 3 5 8 9 3 1 41 6];
suma = cumsum(a);
suma50 = suma(end)/2;
[minv,idxv] = min((suma50-suma).^2)
minv = 2.2500
idxv = 9
.
  3 件のコメント
Star Strider
Star Strider 2021 年 11 月 26 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
Steven Lord
Steven Lord 2021 年 11 月 26 日
If you want the first index that's past the halfway point:
a=[3 5 7 3 5 8 9 3 1 41 6];
suma = cumsum(a)
suma = 1×11
3 8 15 18 23 31 40 43 44 85 91
suma50 = suma(end)/2
suma50 = 45.5000
[~, location] = find(suma > suma50, 1, 'first')
location = 10
suma([location-1 location])
ans = 1×2
44 85

サインインしてコメントする。


Image Analyst
Image Analyst 2021 年 11 月 27 日
Isn't this a duplicate?
If it's not your homework, you can use my solution:
a = [3 5 7 3 5 8 9 3 1 41 6];
s = sum(a)
s = 91
c = cumsum(a)
c = 1×11
3 8 15 18 23 31 40 43 44 85 91
[minDiff, index] = min(abs(c - s/2))
minDiff = 1.5000
index = 9
(If it is your homework, tag it as homework and find a different way. Don't turn in any of our solutions as your own or you may get caught for cheating.)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by