How to find if a cell reaches an average value in a specific amount of instances.

1 view (last 30 days)
I have a cell array thats made up of different vectors. What I want to do is eveluate each vector in the cell to determine if at any point in the vector a mean of a say 10 entries falls within +or - 10 of a value?
  2 Comments
Jan
Jan on 6 Oct 2021
I do not undestand this part: "determine if at any point in the vector a mean of a say 10 entries falls within +or - 10 of a value".

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 6 Oct 2021
Try this:
A= {[11 23 44 99] [111 223 434 939] [1111 2223 433 9339]}
targetValue = 426; % Whatever.
for k = 1 : length(A)
thisVector = A{k};
fprintf('\nVector #%d is', k); disp(thisVector);
thisMean = mean(thisVector);
fprintf(' The mean for vector #%d is %f.\n', k, thisMean);
difference = abs(thisMean - targetValue);
percentDifference = 100 * difference / targetValue;
fprintf(' The difference of the mean from the target value of %.1f is %.2f = %.2f%%.\n', targetValue, difference, percentDifference);
if percentDifference < 10
% It's within 10% of the target value.
fprintf('Yay!!!! Cell #%d has a mean value that is within 10 percent of %.2f.\n', k, targetValue)
end
end
You'll see
Vector #1 is 11 23 44 99
The mean for vector #1 is 44.250000.
The difference of the mean from the target value of 426.0 is 381.75 = 89.61%.
Vector #2 is 111 223 434 939
The mean for vector #2 is 426.750000.
The difference of the mean from the target value of 426.0 is 0.75 = 0.18%.
Yay!!!! Cell #2 has a mean value that is within 10 percent of 426.00.
Vector #3 is 1111 2223 433 9339
The mean for vector #3 is 3276.500000.
The difference of the mean from the target value of 426.0 is 2850.50 = 669.13%.
Obviously the code could be shortened a lot by taking out the informative messages.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by