How can i run 2 for loops at the same time?
15 views (last 30 days)
Show older comments
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 Comments
Image Analyst
on 6 Jun 2020
When you say at the "same time" do you mean like in parallel, like with "parfor" in the Parallel Processing Toolbox?
Answers (1)
the cyclist
on 6 Jun 2020
Edited: the cyclist
on 6 Jun 2020
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 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!