How can I fix random numbers in a for loop?

3 次查看(过去 30 天)
Hello everyone. I am trying to write a code that does something like this:
for k=1:10
j=0
for t=1:.1:10
j=j+1;
s1(j)=randi([1,5],1,1)*sin(1*t)+randi([1,5],1,1)*sin(2*t);
s2(j)=randi([1,5],1,1)*sin(1*t)+randi([1,5],1,1)*sin(2*t);
end
diff(k)=(s1-s2);
end
avgdiff=mean(diff)
but I want to fix the random integer for the entire loop of k. So each time, 1-10, that k is looped, a new random coefficient is generated for each polynomial. As it stands now, it seems like a new random coefficient is generated for each loop of t. I would prefer if it would be generated for each loop of k, such that, for example
diff(1)=[(3*sin(1*t)+2*sin(2*t))-(4*sin(1*t)+2*sin(2*t))]
diff(2)=[(1*sin(1*t)+3*sin(2*t))-(5*sin(1*t)+4*sin(2*t))]
I hope that makes sense. I am still trying to learn, so any feedback is appreciated. Thank you in advance!

采纳的回答

David Hill
David Hill 2021-10-14
t=1:.1:10;
for k=1:10
s1=randi(5)*sin(1*t)+randi(5)*sin(2*t);
s2=randi(5)*sin(1*t)+randi(5)*sin(2*t);
Diff(k)=(s1-s2);
end
avgdiff=mean(Diff);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by