Subtracting the two matrices in the matrix, negative values ​​are replaced by zero as a result

I have two matrices in matlab,

> IRwindow =
> 
>   **183**  171  150  125  137
    138  167  184  173  152
    105  114  141  167  185  
    148  113  105  115  141  
    186  183  147  112  105
> 
> ILwindow =
> 
>   **201**  170  165  177  203
    181  174  167  169  189 
    154  150  156  168  181 
    187  175  158  131  144 
    173  186  183  167  141

I want to subtract these two matrices by elements and get the result; for example, for the first element ( 183 - 201 = -18 ). BUT the output for this element gives zero. The result will be as follows:

> IRwindow - ILwindow

 ans =

     **0**    1    0    0    0
     0    0   17    4    0
     0    0    0    0    4
     0    0    0    0    0    
     13    0    0    0    0

How can I save real results? without getting zero for negatives in my result matrix

+5
source share
3 answers

Run the following code example:

%# Create random matrices
X = randi(100, 5, 5);
Y = randi(100, 5, 5);

%# Convert to strictly non-negative format
X = uint8(X);
Y = uint8(Y);

%# Perform subtractions
A = X - Y;

%# Convert to double format
X = double(X);
Y = double(Y);

%# Perform subtraction
B = X - Y;

For a given sample run:

A =

    0   15   36    0    0
    0    0    0    0    3
    0    0    0   25    0
   13    0   15    0    0
    0   49    0    0   14

and

B =

    -8    15    36    -4   -65
     0   -47   -45   -11     3
   -18   -17   -11    25   -52
    13   -53    15   -15    -1
   -35    49   -47    -8    14

You will notice that all negative numbers in Awere replaced by 0, and negative numbers in are Bdisplayed correctly.

: , , Matlab 0. , , "" ( ), double, , , int, int8, int16, int32 int64.

+6

- single double :

 ans=double(IRwindow-ILwindow) 
+2

I am not getting the same problem as you: I have this code:

IRwindow = [

183  171  150  125  137
138  167  184  173  152
105  114  141  167  185  
148  113  105  115  141  
186  183  147  112  105]

ILwindow = [

201  170  165  177  203
181  174  167  169  189 
154  150  156  168  181 
187  175  158  131  144 
173  186  183  167  141]


IRwindow - ILwindow

and I get this output:

IRwindow =

   183   171   150   125   137
   138   167   184   173   152
   105   114   141   167   185
   148   113   105   115   141
   186   183   147   112   105


ILwindow =

   201   170   165   177   203
   181   174   167   169   189
   154   150   156   168   181
   187   175   158   131   144
   173   186   183   167   141


ans =

   -18     1   -15   -52   -66
   -43    -7    17     4   -37
   -49   -36   -15    -1     4
   -39   -62   -53   -16    -3
    13    -3   -36   -55   -36

Check that you create your matrices, are created properly (as doubles, not as unsigned integers).

0
source

All Articles