Error calculating mean when variable inputs are decimals between 0 and 1
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Maddy
el 10 de Abr. de 2023
Comentada: Maddy
el 10 de Abr. de 2023
I am trying to calculate the mean of 2 invidiual variables within a large cell array of data. My work around for all other variables has been to index the specific value needed and then do the math. For example:
var1 = cell2mat(myArray(5,1));
var2 = cell2mat(myArray(6,1));
avg_var = mean(var1, var2);
However, this has only worked so far when the variables I pull have been whole integers. When trying to replicate this process on another data set, whose values are all between 0 and 1, I get the error:
Error using sum
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.
Error in mean (line 127)
y = sum(x, dim, flag) ./ mysize(x,dim);
Here are my example values:
leftRT = 0.5819;
rightRT = 0.6710;
avg_RT = mean(leftRT, rightRT);
0 comentarios
Respuesta aceptada
Steven Lord
el 10 de Abr. de 2023
The first input to the mean function is the data whose mean you want to compute. The second input, if it is numeric, is the dimension or the vector of dimensions over which you want to compute the mean. You will need to combine your two inputs into one array and pass that larger array into mean as the first input.
x = 1:10;
y = x.^2;
mean([x, y]) % This will work
mean(x, y) % This won't do what you think it will
That latter syntax takes the mean in the 1st, 4th, 9th, etc. dimensions. Since x has size 1 in the 4th, 9th, 16th, etc. dimensions that's equivalent to:
mean(x, 1)
since the mean of one number is that number itself.
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!