what's the difference between | and || in matlab??
448 次查看(过去 30 天)
显示 更早的评论
what's the difference between | and || in matlab??
0 个评论
采纳的回答
更多回答(1 个)
Sean de Wolski
2014-12-10
编辑:Sean de Wolski
2014-12-10
| applies to each element in the array, || applies to a scalar condition:
[1 0 1] | [ 0 0 1]
v.
[1 0 1] || [ 0 0 1]
4 个评论
DGM
2024-12-2,14:52
编辑:DGM
2024-12-2,14:53
I hate to nitpick, but I'm bored and have a minor tweak to add:
The || is a scalar operator, and the | operator is elementwise; that's true. The problem is describing what's expected of an elementwise operator's output size (at least if we're going to explicitly describe it in the context of an ancient question).
Now if we're living in 2014, then the statement that the output is the same size as the input would be true. In the present day, the size of the output of an elementwise operator isn't necessarily the same size as the input. That's only the case when the two inputs are the same size.
Assuming that the inputs have compatible size, the output size is the maximum of input sizes (assuming we're using R2016b or newer (or are using bsxfun() in older versions)).
[1 0 1] | [0 0 1] % output is 1x3
[1 0 1] | [0 0 1].' % output is 3x3
Even without this addendum, I think that @Steve Van Hooser's comment is a more meaningful answer than the original. The short-circuit functionality is really secondary to the fact that || is scalar.
Steve Van Hooser
2024-12-2,15:04
Nice addition. I don't see how to give a thumbs up so I will do it in words.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!