manipulation of seperate vector values

so i have a vector set of values and wanted to round all the values to the nearest positive, even integer. and all the values less then zero are rounded to zero. any idea on how to do this using an mfile?

 채택된 답변

Jan
Jan 2011년 4월 4일

2 개 추천

v(v<0) = 0;
v = round(v/2) * 2;

추가 답변 (2개)

Paulo Silva
Paulo Silva 2011년 4월 4일

1 개 추천

%v is your vector
idx=v<0; %get the index of all values lower than 0
idx1=v>0; %get the index of all values higher than 0
v(idx)=0; %substitute the lower than 0 values by 0
v(idx1)=round(v(idx1)) %substitute higher than 0 values by nearest positive
In case you want a special form of the round function that does nearest positive and even integer get the round2even function or round2

댓글 수: 3

Jan
Jan 2011년 4월 4일
Or shorter: v = round(v); v(v<0)=0;
Paulo Silva
Paulo Silva 2011년 4월 4일
That's shorter but does unnecessary rounding of the negative values
Jan
Jan 2011년 4월 4일
Correct. Therefore your method will be faster, if a lot of negative numbers occur.

댓글을 달려면 로그인하십시오.

Yusuf
Yusuf 2011년 4월 4일

0 개 추천

yes thanks that works but i need it to round to the nearest even integer?

댓글 수: 1

Jan
Jan 2011년 4월 4일
Ah, now I understand the "even integer" in your question.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2011년 4월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by