For loop count question
显示 更早的评论
I have a count question regarding for loop.
May I ask how to get the "count" number representing from 1 to 86625?
Or there is a better to code? Thank you!
a1=-70:5:100;
a2=-60:5:60;
a3=-50:10:50;
a4=-40:10:40;
U1=1*ones(100,1);
U2=2*ones(100,1);
U3=3*ones(100,1);
U4=4*ones(100,1);
x=0.5*ones(100,1);
y=zeros(100,1);
z=2*ones(100,1);
diff=zeros(length(a1)*length(a2)*length(a3)*length(a4),5);
% length(a1)*length(a2)*length(a3)*length(a4)=35*25*11*9=86625
for i=1:length(a1)
for ii=1:length(a2)
for iii=1:length(a3)
for iiii=1:length(a4)
y=x+a1(i)*U1+a2(ii)*U2+a3(iii)*U3+a4(iiii)*U4;
count=? % This goes from 1 to 86625 represented by i, ii, iii and iii
diff(count,1)=sqrt(sum((z-y).^2)/length(U1));
diff(count,2)=a1(i);
diff(count,3)=a2(ii);
diff(count,4)=a3(iii);
diff(count,5)=a4(iiii);
end
end
end
end
采纳的回答
更多回答(1 个)
Rik
2019-4-18
0 个投票
Initialize count to 0 before your nested loop and do count=count+1; inside the inner loop.
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!