manipulation of seperate vector values

10 visualizaciones (últimos 30 días)
Yusuf
Yusuf el 4 de Abr. de 2011
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?

Respuesta aceptada

Jan
Jan el 4 de Abr. de 2011
v(v<0) = 0;
v = round(v/2) * 2;

Más respuestas (2)

Paulo Silva
Paulo Silva el 4 de Abr. de 2011
%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 comentarios
Paulo Silva
Paulo Silva el 4 de Abr. de 2011
That's shorter but does unnecessary rounding of the negative values
Jan
Jan el 4 de Abr. de 2011
Correct. Therefore your method will be faster, if a lot of negative numbers occur.

Iniciar sesión para comentar.


Yusuf
Yusuf el 4 de Abr. de 2011
yes thanks that works but i need it to round to the nearest even integer?
  1 comentario
Jan
Jan el 4 de Abr. de 2011
Ah, now I understand the "even integer" in your question.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by