I'm confused with the matlab operator |
. Could you say what does it actually mean?
Suppose that I've 2 image matrices image1
and image2
so what would image3=image1|image2;
mean?
Thanks
I'm confused with the matlab operator |
. Could you say what does it actually mean?
Suppose that I've 2 image matrices image1
and image2
so what would image3=image1|image2;
mean?
Thanks
This operator stands for element-wise logical or operation: see doc.
Suppose image1
and image2
are logical matrices (with entries either true
or false
), then image3 = image1 | image2;
means that each entry in image3
is a logical or of the respective entries in image1
and image2
Element-wise logical OR operation
so:
[1 0] | [1 1]
would result in [ 1 1 ]
and,
[0 1] | [0 0]
would result in [ 0 1 ]
.
In your case image3
would be a matrix of the size of image1
and image2
holding trues (1) or falses as obtained by a element-wise logical OR.
By extension of operators used in early languages (C, C++), broadly, in MATLAB which is derived from C, |
has the standard meaning, that is, OR
operator of boolean logic.
As for your comment about element-wise multiplication or division in matrix we use . as well, but for what purpose do we use |?
, if we use .|
, then it is equivalent to |
, just like .+
and .-
. All these operators require the operands to be of equal size. But, historically due to the same symbol for normal multiplication and matrix multiplication, there are two symbols, *
and .*
respectively. These symbols *
and .*
are totally different, so as to avoid the ambiguity in logic of normal multiplication and matrix multiplication. Similarly, it is ditto for division operation.
Equivalent operations:
.+
== +
.-
== -
.|
== |
Not equivalent operations:
.*
!= *
./
!= /
Assuming you have two equally sized matrices image1
and image2
(can contain logicals but can also contain other values)
Then image3 = image1 | image2
will give you the so called 'logical mask' of image1
and image2
.
This means that image 3 is equal to 1 (true) at points where at least one of the images is a nonzero number, and equal to 0 (false) if they are both zero.
Example:
image1 = [ 0 255;
166 0]
image2 = [-123 0;
255 0]
image3 = image1 | image2
% Will give as output:
[1 1
1 0]