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

7 ビュー (過去 30 日間)
Yan Yan
Yan Yan 2019 年 3 月 6 日
回答済み: Pranjal Priyadarshi 2019 年 3 月 14 日
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 件)

Pranjal Priyadarshi
Pranjal Priyadarshi 2019 年 3 月 14 日
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

カテゴリ

Help Center および File ExchangeGeology についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by