MINUS Subtraction Operator

Section: Mathematical Operators

Usage

Subtracts two numerical arrays (elementwise). There are two forms for its use, both with the same general syntax:
  y = a - b

where a and b are n-dimensional arrays of numerical type. In the first case, the two arguments are the same size, in which case, the output y is the same size as the inputs, and is the element-wise difference of a and b. In the second case, either a or b is a scalar, in which case y is the same size as the larger argument, and is the difference of the scalar to each element of the other argument. The type of y depends on the types of a and b using the type promotion rules. The types are ordered as:

  1. uint8 - unsigned, 8-bit integers range [0,255]
  2. int8 - signed, 8-bit integers [-127,128]
  3. uint16 - unsigned, 16-bit integers [0,65535]
  4. int16 - signed, 16-bit integers [-32768,32767]
  5. uint32 - unsigned, 32-bit integers [0,4294967295]
  6. int32 - signed, 32-bit integers [-2147483648,2147483647]
  7. float - 32-bit floating point
  8. double - 64-bit floating point
  9. complex - 32-bit complex floating point
  10. dcomplex - 64-bit complex floating point
Note that the type promotion and combination rules work similar to C. Numerical overflow rules are also the same as C.

Function Internals

There are three formulae for the subtraction operator, depending on the sizes of the three arguments. In the most general case, in which the two arguments are the same size, the output is computed via:

If a is a scalar, then the output is computed via

On the other hand, if b is a scalar, then the output is computed via

Examples

Here are some examples of using the subtraction operator. First, a straight-forward usage of the minus operator. The first example is straightforward - the int32 is the default type used for integer constants (same as in C), hence the output is the same type:
--> 3 - 8

ans = 

 -5 


Next, we use the floating point syntax to force one of the arguments to be a double, which results in the output being double:

--> 3.1 - 2

ans = 

    1.1000 


Note that if one of the arguments is complex-valued, the output will be complex also.

--> a = 3 + 4*i

a = 

    3.0000 +  4.0000i 

--> b = a - 2.0f

b = 

    1.0000 +  4.0000i 


If a double value is subtracted from a complex, the result is promoted to dcomplex.

--> b = a - 2.0

b = 

    1.0000 +  4.0000i 


We can also demonstrate the three forms of the subtraction operator. First the element-wise version:

--> a = [1,2;3,4]

a = 

 1 2 
 3 4 

--> b = [2,3;6,7]

b = 

 2 3 
 6 7 

--> c = a - b

c = 

 -1 -1 
 -3 -3 


Then the scalar versions

--> c = a - 1

c = 

 0 1 
 2 3 

--> c = 1 - b

c = 

 -1 -2 
 -5 -6 


inserted by FC2 system