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

조회 수: 4 (최근 30일)
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 CenterFile Exchange에서 Geology에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by