Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

problem in vectorize script

1 visualización (últimos 30 días)
Mani Ahmadian
Mani Ahmadian el 19 de Oct. de 2014
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Hi
I want to improve my script as:
a=round(50*rand(5,7)+50);
for ii=1
for jj=1
if a(ii,jj)>= 60
b1=2*a(ii,jj)
else
b2=-1*a(ii,jj)
end
end
end
b=b1+b2;
to vetcorized. How can I do that?
Thanks a lot
Mani
  1 comentario
Jan
Jan el 19 de Oct. de 2014
for ii=1 runs the loop over one element only. Do you mean for ii=1:5 ?

Respuestas (2)

Rick Rosson
Rick Rosson el 19 de Oct. de 2014
a = round(50*rand(5,7)+50);
b = 2*a;
b(a<60) = -a;
  1 comentario
Jan
Jan el 19 de Oct. de 2014
The right hand sige need to be masked also:
b(a<60) = -a(a<60);

Jan
Jan el 19 de Oct. de 2014
Or as a one-liner:
b = a .* ((a >= 60) * 3 - 1);

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by