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

65 Ansichten (letzte 30 Tage)
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.
  1 Kommentar
Geoff Hayes
Geoff Hayes am 18 Jul. 2019
Manan - are you sure that your mean is actually 0.01 or is that just how the value that is displayed? See Set Command Window output display format for details.
Also, is
mean(x(2:end,1),x(1:end-1,1))
valid? Can you pass two arrays as inputs to the mean function?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Steven Lord
Steven Lord am 18 Jul. 2019
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

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by