フィルターのクリア

Sum of elements in an array over x

6 ビュー (過去 30 日間)
Jacob Merriman
Jacob Merriman 2020 年 6 月 10 日
コメント済み: Turlough Hughes 2020 年 6 月 10 日
Hello all,
I am looking to find the sum of elements in an array which exceed a certain value, e.g. 0.05.
I have this array: [0.023 0.056 0.053 0.034 0.021 0.075 0.088]
As you can see, 4 elements are over 0.05. However, I don't want to know 'how many elements' are over 0.05, I want to find the cumulative sum of the elements which match my criteria.
In this case: 0.056 + 0.053 + 0.075 + 0.088.
Total = 0.272
Can anyone help?
kind regards,
Jacob
  2 件のコメント
Jacob Merriman
Jacob Merriman 2020 年 6 月 10 日
編集済み: Jacob Merriman 2020 年 6 月 10 日
Just to let people know, I found a way to solve this by simply deleting the elements in the array which were less than 0.05 and creating a new variable. Then we can simply sum the new variable.
Turlough Hughes
Turlough Hughes 2020 年 6 月 10 日
As a point of clarification, you are looking to add elements where each individual element exceeds a value of 0.05?

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

採用された回答

Turlough Hughes
Turlough Hughes 2020 年 6 月 10 日
編集済み: Turlough Hughes 2020 年 6 月 10 日
a = [0.023 0.056 0.053 0.034 0.021 0.075 0.088];
result = sum(a(a>0.05))
  5 件のコメント
Turlough Hughes
Turlough Hughes 2020 年 6 月 10 日
No problem, you to. I added a follow up comment just to remove any ambiguity.
Turlough Hughes
Turlough Hughes 2020 年 6 月 10 日
I think the wording is a bit ambiguous and could be interpreted both ways.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 6 月 10 日
In general, this will do it. You need to use combnk() to get all possible combinations of elements added together. Then check the sum of each combination:
v = [0.023 0.056 0.053 0.034 0.021 0.075 0.088]
threshold = 0.05;
counter1 = 1;
counter2 = 1;
rowSum = 0;
for k = 1 : length(v)
indexes = combnk(1 : length(v), k); % Get all combinations.
[rows, columns] = size(indexes);
rowSum = rowSum + rows;
for row = 1 : rows
% For this particular combination...
thisRow = indexes(row, :);
theSum(counter1) = sum(v(thisRow));
% See if this sum is more than the threshold
if theSum(counter1) > threshold
fprintf('These %d elements sum to more than %.2f\n ', length(thisRow), threshold);
fprintf('%.3f + ', v(thisRow(1 : end-1)));
fprintf('%.3f = %.3f\n', v(thisRow(end)), theSum(counter1));
counter2 = counter2 + 1;
end
counter1 = counter1 + 1;
end
end
histogram(theSum)
grid on;
caption = sprintf('Histogram of sums after %d combinations', rowSum);
title(caption, 'FontSize', 20);
xlabel('Sum', 'FontSize', 20);
ylabel('Count', 'FontSize', 20);
You'll see 124 (counter2) lines in the command window printing out the sums that are more than your threshold, like these:
These 6 elements sum to more than 0.05
0.023 + 0.056 + 0.034 + 0.021 + 0.075 + 0.088 = 0.297
These 6 elements sum to more than 0.05
0.023 + 0.053 + 0.034 + 0.021 + 0.075 + 0.088 = 0.294
These 6 elements sum to more than 0.05
0.056 + 0.053 + 0.034 + 0.021 + 0.075 + 0.088 = 0.327
These 7 elements sum to more than 0.05
0.023 + 0.056 + 0.053 + 0.034 + 0.021 + 0.075 + 0.088 = 0.350

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by