rounding given matrix towards zero and even number??

Im given a row matrix which contain negative and decimals values. Im asked to round those negative values into zero and those positive with decimals values to even number using for loops and if statement. how can i accomplish this

 Respuesta aceptada

Here's a start:
a = 10 * rand(1,15)-5
for k = 1 : length(a)
if a(k) < 0
a(k) = 0; % Set negative numbers to 0
else
% Round to nearest multiple of 2
end
end
Since it sounds like homework, I'll let you do the part on rounding to the nearest multiple of 2.

2 comentarios

Victor Seaw
Victor Seaw el 20 de Abr. de 2015
Yes it is homework I don't have Matlab v me so can I type out the code here and hopefully u can tell me whether its an error or will it run. Else.. a=2*round(a/2)..?? Will this work in a if and for case
Here's some results with your formula:
a =
Columns 1 through 11
-3.947 -2.9348 -1.9222 -0.9382 0.050209 1.0485 2.011 3.0917 4.0433 5.0427 6.0029
Columns 12 through 17
7.051 8.0964 9.0275 10.074 11.012 12.077
a =
0 0 0 0 0 2 2 4 4 6 6 8 8 10 10 12 12

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 19 de Abr. de 2015

Comentada:

el 20 de Abr. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by