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

1 visualización (últimos 30 días)
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 comentarios
C B
C B el 6 de Oct. de 2021
can you Please eloobrate what ipnut and your expected output?
like this
input should be
A= {[11 23 44 99] [111 223 434 939] [1111 2223 433 9339]}
Jan
Jan el 6 de Oct. de 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".

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 6 de Oct. de 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.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by