ho to find index at which the 50% of the sum vector is included?
1 次查看(过去 30 天)
显示 更早的评论
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
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)
.
3 个评论
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)
suma50 = suma(end)/2
[~, location] = find(suma > suma50, 1, 'first')
suma([location-1 location])
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)
c = cumsum(a)
[minDiff, index] = min(abs(c - s/2))
(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.)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!