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

65 ビュー (過去 30 日間)
Manan Patel
Manan Patel 2019 年 7 月 18 日
回答済み: Steven Lord 2019 年 7 月 18 日
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 件のコメント
Geoff Hayes
Geoff Hayes 2019 年 7 月 18 日
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?

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2019 年 7 月 18 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by