Repetition of values number of times

2 Ansichten (letzte 30 Tage)
Offroad Jeep
Offroad Jeep am 4 Feb. 2016
Kommentiert: Guillaume am 5 Feb. 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 Kommentare
Guillaume
Guillaume am 5 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Kelly Kearney
Kelly Kearney am 4 Feb. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Audio Processing Algorithm Design finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by