0
 H = [1 1; 1 2; 2 -1; 2 0; -1 2; -2 1; -1 -1; -2 -2;]';

I need to threshold each value such that

H(I,j) = 0 if H(I,j) > =1,
else H(I,j) = 1 if H(I,j) <=0

I applied this code

  a = H(1,1) 
    a(a<=0) = 1

    a(a>=1) = 0

But this means that the already affected value in the first step may get changed again. What is the correct way of thresholding? The above code is giving incorrect answers. I should be getting

a = [0 0; 0 0; 0 1;  0 1;  1 0;  1 0;   1 1;   1 1]

Please help

EDIT

Based upon the answer now I am getting

         0         0
         0         0
    1.0000    0.3443
    0.8138    0.9919
         0    0.7993
    0.1386    1.0000
    1.0000    1.0000
    1.0000    1.0000

As can be seen, rows 3-6 are all incorrect. Please help

Community
  • 1
  • 1
SKM
  • 959
  • 2
  • 19
  • 45

1 Answers1

0
ind1 = H>=1; %// get indices before doing any change
ind2 = H<=0;
H(ind1) = 0; %// then do the changes
H(ind2) = 1;

If dealing with non-integer values, you should apply a certain tolerance in the comparisons:

tol = 1e-6; %// example tolerance
ind1 = H>=1-tol; %// get indices before doing any change
ind2 = H<=0+tol;
H(ind1) = 0; %// then do the changes
H(ind2) = 1;
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • Thank you for your reply, which works but not for all cases. For instance, there are some real number values which are getting unaffected. I have updated my question above. Please have a look at the real number values and how do I limit them in the range> – SKM Mar 30 '14 at 00:13
  • @SKM That's probably [the usual problem with comparing floating values](http://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab). To solve it, define a tolerance and apply it in the comparisons. See updated answer (second part) – Luis Mendo Mar 30 '14 at 02:55
  • I have done like this asI guess mythreshold function was incorrect. Is it now correct instead of applying the tolerance? ind1 = H>0; ind2 = H<=0; H(ind1) = 0; H(ind2) = 1; – SKM Mar 30 '14 at 03:03
  • @SKM I don't know if it's correct because I don't know what you're trying to achieve. But when comparing real (floating point) numbers you sould always apply a tolerance – Luis Mendo Mar 30 '14 at 03:06
  • I am trying to apply various thresholding function used to squash the outputs of neural network. Thresholding functions like Sigmoid, hard limit etc. For instance, in hard limit function the output should only be binary [0,1]; whereas sigmoid gives continuous real values. – SKM Mar 30 '14 at 03:09