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);
댓글 수: 0
채택된 답변
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
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!