フィルターのクリア

How to write an if statement that checks if array is real and positive?

2 ビュー (過去 30 日間)
Brian Aguilar
Brian Aguilar 2016 年 2 月 27 日
コメント済み: Walter Roberson 2016 年 2 月 27 日
I have an array called amplitude with an N*1 matrix, N is a positive and integer number
This is my code:
if sum(A<0)~=0 || (A~=isreal(A))
error('Amplitude must be real and postive elements');
end
it doesn't seem to work, could use help?

採用された回答

Geoff Hayes
Geoff Hayes 2016 年 2 月 27 日
編集済み: Geoff Hayes 2016 年 2 月 27 日
Brian - since A is an array, you will want to use the all function to ensure that all elements are positive and all elements are real. For example,
if ~all(isreal(A)) || ~all(A>0)
error('Amplitude must have only real and positive elements');
end
Try the above and see what happens!
With your code, with sum it seems like you want to check to see if the sum of all negative elements is not zero which may be okay, but should be discouraged when comparing singles or doubles to an integer (use a tolerance instead). And your second condition is trying to compare all elements of A against an array of ones and zeros (with a one indicating a real number). Why?
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 2 月 27 日
Hypothetically,
any(A(:)<=0)
could be faster than
~all(A>0)
Watch out for the fact that A is 2D but all() and any() act on a single dimension.

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

その他の回答 (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