Sum of elements in an array over x

12 Ansichten (letzte 30 Tage)
Jacob Merriman
Jacob Merriman am 10 Jun. 2020
Kommentiert: Turlough Hughes am 10 Jun. 2020
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 Kommentare
Jacob Merriman
Jacob Merriman am 10 Jun. 2020
Bearbeitet: Jacob Merriman am 10 Jun. 2020
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 am 10 Jun. 2020
As a point of clarification, you are looking to add elements where each individual element exceeds a value of 0.05?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Turlough Hughes
Turlough Hughes am 10 Jun. 2020
Bearbeitet: Turlough Hughes am 10 Jun. 2020
a = [0.023 0.056 0.053 0.034 0.021 0.075 0.088];
result = sum(a(a>0.05))
  5 Kommentare
Turlough Hughes
Turlough Hughes am 10 Jun. 2020
No problem, you to. I added a follow up comment just to remove any ambiguity.
Turlough Hughes
Turlough Hughes am 10 Jun. 2020
I think the wording is a bit ambiguous and could be interpreted both ways.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 10 Jun. 2020
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

Community Treasure Hunt

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

Start Hunting!

Translated by