Error calculating mean when variable inputs are decimals between 0 and 1

1 visualizzazione (ultimi 30 giorni)
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);

Risposta accettata

Steven Lord
Steven Lord il 10 Apr 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
ans = 22
mean(x, y) % This won't do what you think it will
ans = 1×10
1 2 3 4 5 6 7 8 9 10
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)
ans = 1×10
1 2 3 4 5 6 7 8 9 10
since the mean of one number is that number itself.

Più risposte (0)

Tag

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by