rounding of the numbers in a array as per a condition

조회 수: 4 (최근 30일)
Chandradhar Savanth
Chandradhar Savanth 2013년 11월 3일
댓글: Chandradhar Savanth 2013년 11월 3일
hi..
suppose i have an array x=[1 ,2, -2.2, 2.2, -3, 4];
all the elements of the above array should satisfy the condition that they should lie in the interval [-2 2];
if any element is less than '-2' then i should round-off it to '-2'.
if any element is greater than '2' then i should round-off it to '2'.
but remaining elements should be unchanged.
so,finally i should get the answer as x = [1 ,2 ,-2 ,2, -2, 2];
is there a smart coding way with out using loops.
Regards.
Chandradhar Savanth...

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 11월 3일
max(min(x,2),-2)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by