change a vector into two different value by compare element in it with a threshold
    6 views (last 30 days)
  
       Show older comments
    
is there a matlab function that can change a vector into two different value by compare element in it with a threshold?
function like this:
p = round(p); %change 1.4 to 1;1.6 to 2....
Answers (1)
  Pranjal Priyadarshi
      
 on 14 Mar 2019
        We can achieve it by writing the vector in the following format. 
p(p>threshold) = m; %m and n being any number which we want the vector elements to replace with. 
p(p<=threshold) = n;
To be clearer you can follow this example:
>> p = [0.2,0.4,0.5,0.6,1.8,2.1,2.2,2.6,3,3.5,3.3,3.8,1,1.2,1.4,1.5,1.6,1.7];
>> p(p<0.5) = 0;
>> p(p>=0.5 & p<1.5) = 1;
>> p(p>=1.5 & p<2.5) = 2;
>> p(p>=2.5 & p<3.5) = 3;
>> p(p>=3.5) = 4;
>>p
p =
  Columns 1 through 15
     0     0     1     1     2     2     2     3     3     4     3     4     1     1     1
  Columns 16 through 18
     2     2     2
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

