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

1 次查看(过去 30 天)
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);

采纳的回答

Steven Lord
Steven Lord 2023-4-10
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by