Repetition of values number of times

조회 수: 1 (최근 30일)
Offroad Jeep
Offroad Jeep 2016년 2월 4일
댓글: Guillaume 2016년 2월 5일
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
Guillaume
Guillaume 2016년 2월 5일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Kelly Kearney
Kelly Kearney 2016년 2월 4일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by