MATLAB: Mean value of two variables from an array with less than condition.

less thanmean

if mean(x(2:end,1),x(1:end-1,1)) < 0.01
do something;
end
For some cases my mean value is actually 0.01 but it is still going into the loop. I am not sure why. I have also tried lt(mean(x(2:end,1),x(1:end-1,1)),0.01) but it is still not working for me.
Thank you in advance for your help.

Best Answer

Geoff Hayes: as of release R2018b, as long as all the elements of x(1:end-1, 1) are positive integer values, it's valid ... but it's probably not doing what the original poster expected. It would treat those elements as the dimensions over which to take the mean of x(2:end, 1).
Manan Patel: if you want to take the mean of adjacent elements of x's first row, that's not the right syntax with which to do it. You could concatenate two shifted copies of the rows together and take the mean of that matrix in the appropriate dimension, but that would result in a vector of values.You need to be careful when specifying a vector of values as the expression for an if statement. "An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false." You need to use any if you want the statement to be executed if any of the means are less than 0.01.
x = (1:10).' % Sample data
M = [x(2:end, 1), x(1:end-1, 1)]
meanvalues = mean(M, 2)
Alternately, since you know what the mean of two numbers is (it's their sum divided by 2) you could hard-code that. But you'll still need to be careful with your if.
x = (1:10).';
meanvalues2 = (x(2:end, 1)+x(1:end-1, 1))/2