フィルターのクリア

If Condition with array of elements

50 ビュー (過去 30 日間)
Uday padidela
Uday padidela 2014 年 9 月 26 日
回答済み: David Young 2014 年 9 月 28 日
Can anyone help me how to write this condition
if (ra >= 1.12246*sigma)
uw=0.0;
end
Here my ra is any array of elements like and the condition should satisfy each element of an array.
So how should i write it

採用された回答

Guillaume
Guillaume 2014 年 9 月 26 日
編集済み: Guillaume 2014 年 9 月 26 日
Use all (or any if you just want some elements to statisfy the condition)
if all(ra >= 1.12246*sigma)
%...
end
Note, this assume ra is a vector and sigma is scalar. If ra is a matrix:
if all(ra(:) >= 1.12246*sigma)
%...
end
  3 件のコメント
Guillaume
Guillaume 2014 年 9 月 28 日
You'll have to be clearer than 'it's not working'.
What are the values of ra and sigma_, and what result do you expect?
Uday padidela
Uday padidela 2014 年 9 月 28 日
編集済み: Uday padidela 2014 年 9 月 28 日
I have given the details of my code in the thread If condition is not working
My ra values are from 0 to 8.7345 with interval of 0.1066.
And my sigma have 3 values 32, 16.15 and 0.3.
I have used this condition in my potential.
My potential should decay and die off to zero on long run of my ra, but its not so.
Thank you

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

その他の回答 (2 件)

Stephen23
Stephen23 2014 年 9 月 26 日
編集済み: Stephen23 2014 年 9 月 26 日
Actually you don't need to do anything. In MATLAB, the if function already is defined to be satisfied IFF every element of the conditional array is non-zero.
The documentation states this clearly "An evaluated expression is true when the result is nonempty and contains all nonzero elements (logical or real numeric). Otherwise, the expression is false."
So you example will calculate the code within the if, only if all of (ra >= 1.12246*sigma) are true.
If you only need some of them to be true, then you will need to use an any.
  1 件のコメント
Uday padidela
Uday padidela 2014 年 9 月 28 日
Thank you for the answer.
But the condition is working.

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


David Young
David Young 2014 年 9 月 28 日
You should try to state more clearly what is not working. Give an example set of initial values, say what you expect the result to be, and say what result you actually get. Without this, it's really hard to help you.
All the same, here's a guess. Maybe you need:
uw(ra >= 1.12246*sigma) = 0.0;
This applies the test to each element of ra and sets the corresponding element of uw to zero if the element of ra passes the test.

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by