What does +(A>0) do?

1 回表示 (過去 30 日間)
Mary
Mary 2011 年 10 月 11 日
I'm an experienced Matlab user, but I came across a line of code that I don't understand:
A = +(A>0)
I know that the A>0 will find indices for values of A greater than zero, but any thoughts on what the +() does?
Thanks!

採用された回答

David Young
David Young 2011 年 10 月 11 日
Looks like someone is forcing the class of A to be double rather than logical.
A = double(A>0)
would have the same effect but would be much clearer, and therefore preferable.
  2 件のコメント
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 11 日
That's a good argument, in terms of changing the data type of A.
David Young
David Young 2011 年 10 月 11 日
Though I suppose if MATLAB ever changed its default numerical class from double to something else, the form +() would continue to mean "convert to default numerical class", whereas double() would convert to a specific but outdated class. This is, however, an unlikely scenario!

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

その他の回答 (3 件)

Wolfgang Schwanghart
Wolfgang Schwanghart 2011 年 10 月 11 日
I often do this to convert a logical matrix to double. However, I just tried and realized that this is slower than A = double(A>0).
A = rand(4000,4000)>0.5;
>> tic; B = double(A); toc
Elapsed time is 0.041147 seconds.
>> tic; B = +(A); toc
Elapsed time is 0.053846 seconds.
Again, I have learned something here.
  1 件のコメント
Mary
Mary 2011 年 10 月 11 日
Thanks -- this answer was also helpful and it's good to know which method is faster. I appreciate you taking the time to comment.

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


Fangjun Jiang
Fangjun Jiang 2011 年 10 月 11 日
The "+" doesn't mean anything here. Probably somebody with C++ experience wrote it. You can try this example:
A=rand(3)-0.5;
A=+(A>0);
Think of it as
A=0+(A>0)

Amey
Amey 2011 年 10 月 11 日
In the matrix A, it indicates which values are greater than zero. For example: A = [-3 -1 0 9 4 3 2]; The output of the command b = +(A>0) is: b = [0 0 0 1 1 1 1]
Below is another example for the operation. Consider the same matrix A = [-3 -1 0 9 4 3 2]; The output of the command b = +(A>-2) is: b = [0 1 1 1 1 1 1].

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by