# change a vector into two different value by compare element in it with a threshold

2 vues (au cours des 30 derniers jours)
Yan Yan le 6 Mar 2019
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....
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Alex Mcaulley le 6 Mar 2019
a(a>=threshold) = 1;
a(a<threshold) = 0;

Connectez-vous pour commenter.

### Réponses (1)

Pranjal Priyadarshi le 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 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Image Thresholding dans Help Center et File Exchange

R2018b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by