How can i run 2 for loops at the same time?
15 次查看(过去 30 天)
显示 更早的评论
My problem with the code below is that i can't run the two for loops at the same time.I want to check for j=1 and p=1 which of the if conditions is true and do the actions,then check for j=2 and p=4 etc.But the way it is right now , it will check for j=1 and for every p possible untill it changes for j=2.
Any help will be much appreciated
function z=coder(y,d)
ymin=min(y);
Number_samples=length(y);
z=zeros(1,Number_samples*3);
for j=1:Number_samples
for p=1:3:Number_samples*3-2
if y(j)==ymin
z(1,p)=0;
z(1,p+1)=0;
z(1,p+2)=0;
elseif y(j)==ymin+d
z(1,p)=0;
z(1,p+1)=0;
z(1,p+2)=1;
elseif y(j)==ymin+2*d
z(1,p)=0;
z(1,p+1)=1;
z(1,p+2)=0;
elseif y(j)==ymin+3*d
z(1,p)=0;
z(1,p+1)=1;
z(1,p+2)=1;
elseif y(j)==ymin+4*d
z(1,p)=1;
z(1,p+1)=0;
z(1,p+2)=0;
elseif y(j)==ymin+5*d
z(1,p)=1;
z(1,p+1)=0;
z(1,p+2)=1;
elseif y(j)==ymin+6*d
z(1,p)=1;
z(1,p+1)=1;
z(1,p+2)=0;
end
end
end
end
3 个评论
Image Analyst
2020-6-6
When you say at the "same time" do you mean like in parallel, like with "parfor" in the Parallel Processing Toolbox?
回答(1 个)
the cyclist
2020-6-6
编辑:the cyclist
2020-6-6
From your reply to my comment, it seems to me that you really do only need one for loop. Instead of your
for j=1:Number_samples
for p=1:3:Number_samples*3-2
if y(j)==ymin
...
you instead just need
for j=1:Number_samples
p = j+3; % No for loop here. Just need p to be j+3 in each j-loop
if y(j)==ymin
...
That will give the (i,j) pattern
(1,1)
(2,4)
(3,7)
(4,10)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!