Borrar filtros
Borrar filtros

Repetition of values number of times

1 visualización (últimos 30 días)
Offroad Jeep
Offroad Jeep el 4 de Feb. de 2016
Comentada: Guillaume el 5 de Feb. de 2016
Attach please find the code . it is repeatedly giving the value where i only want one time random dtheta and it should not repeat the value for 25 times... Thanks
  2 comentarios
Guillaume
Guillaume el 5 de Feb. de 2016
Most likely, they are related. In either case, the code is full of mistakes and it's impossible to know the original intent. So the answer is the same, if you can't see what is wrong with your code, step through it with the debugger.

Iniciar sesión para comentar.

Respuesta aceptada

Kelly Kearney
Kelly Kearney el 4 de Feb. de 2016
The code (in the future, it's more convenient for readers if you post short code snippets like this, rather than including them as attachments):
m = 5
B = linspace(2,0,10)
theta = 0
nmc = 25
dtheta = [0 rand(1,nmc-2)*5]
for i = 1:length(B)
for j = 1:nmc
theta_new = theta+dtheta
mag_new = m * cosd(theta_new)
end
end
What exactly do you want this code to be doing? The loops right now are completely unnecessary; nothing changes in the loop, so you're just repeating the same calculation 250 times. Add some semicolons to suppress the (repeated) output, and drop the loops:
m = 5;
B = linspace(2,0,10);
theta = 0;
nmc = 25;
dtheta = [0 rand(1,nmc-2)*5];
theta_new = theta+dtheta;
mag_new = m * cosd(theta_new);
However, in this version, several of your variables ( B, nmc) are unused.

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by