2

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

Shai
  • 111,146
  • 38
  • 238
  • 371
the_naive
  • 2,936
  • 6
  • 39
  • 68
  • 2
    You should read the [official documentation](http://www.mathworks.com/help/matlab/matlab_prog/operators.html#f0-38948) for MATLAB logical operators... – Eitan T May 13 '13 at 13:45

4 Answers4

2

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

Shai
  • 111,146
  • 38
  • 238
  • 371
  • So, I understand it's element-wise but for what operation? I mean like for element-wise multiplication or division in matrix we use `.` as well, but for what purpose do we use `|`? – the_naive May 13 '13 at 13:43
  • 2
    @the_naive since logical operations (`|`, `&`) has no "matrix" interpretation, then they don't have the explicit element-wise dot in fornt of them. **But** you should note that `|`,`&` are DIFFERENT than `||` and `&&`! – Shai May 13 '13 at 13:53
  • 1
    FYI, related questions: [What's the difference between & and && in MATLAB?](http://stackoverflow.com/questions/1379415) and [What's the difference between | and || in MATLAB?](http://stackoverflow.com/questions/14183385) – Eitan T May 13 '13 at 15:28
2

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.

Shai
  • 111,146
  • 38
  • 238
  • 371
Nick
  • 3,143
  • 20
  • 34
1

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:

.* != *

./ != /

RAM
  • 2,413
  • 1
  • 21
  • 33
1

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]
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122