how can i simplify this for loop?

for i=1:2 %for-loop1
for j=1:5 %for-loop2
x(j)=i;
y(j)=j+1;
t=[x; y]';
end
for j=1:5 %for-loop3
t(j,1)=t(j,1)+j;
t(j,2)=t(j,2)+2*j;
end
if i==1 %if
a=t;
elseif i>1
a=[a; t];
end
end
disp(a);
thanks for your advice and help.
---------------------------
OHHHH.. I'm sorry for my stupid question.
upper code is simplification, and
for i=1:10 %for-loop1
for j=1:11 %for-loop2
ca=Gimg(round((i-8/10)*H):round((i-2/10)*H),round((j-8/10)*W):round((j-2/10)*W));
if mean(mean(ca))<60
ca(ca>mean(mean(ca))-3)=0;
else
ca(ca<mean(mean(ca))+3)=0;
end
[x(j) y(j)]=pjm_centroid(ca);
array=[x; y]';
end
for j=1:11 %for-loop3
array(j,1)=array(j,1)+round((j-8/10)*W);
array(j,2)=array(j,2)+round((i-8/10)*H);
end
if i==1 %if
p=array;
elseif i>1
p=[p; array];
end
end
disp(p);
---------------------
this is an originally code.
variables i and j are meaningful I think.
When you compare simplification and of original code,
how can I simplify structure of for-loop in original code?

 Réponse acceptée

kjetil87
kjetil87 le 8 Août 2013
Modifié(e) : kjetil87 le 8 Août 2013

0 votes

a=[];
t=zeros(5,2);
for i=1:2
t(:,1)=(1:5)+i;
t(:,2)=(2:6)+2*(1:5);
a=[a;t];
end
disp(a)
You can also get rid of the for i=1:2 but i included it to show the process.
a=zeros(10,2);
a(:,1)=[(2:6),(3:7)];
a(:,2)=repmat((2:6)+2*(1:5),1,2); %or just hardcode
%t(:,2)=[(2:6)+2*(1:5),(2:6)+2*(1:5)]
disp(a)

3 commentaires

After your edit: I do not know how the Gimg funciton works, but if it can take a vectorized inputs you may try to use the same strategy (i.e remove the for loop and replace j with (1:11) . If so the mean test can probably be performed on all outpus at once.
It will atleast work for for-loop3, just remember to pre allocate array and index it with array(:,1) etc.
then
array(:,1)=array(:,1)+round( ( (1:11)-8/1) *W)
will be the same as your third loop etc.
Also your if i==1 test is not needed if you declare p as empty before the loop
p=[];
Then in p=[p;array] will work even when i=1.
In general if you have a for loop that uses the index on each element in an array with the "same operation every time" you can replace the entire loop with a vector such as 1:11 instead of j.
Also from a closer look at your code you may have intended to move
array=[x,y]';
below the end line of for loop 2. Now you are doing 10 meaningless operations before you overwrite it again with the correct result of x and y.
PJM
PJM le 8 Août 2013
Thank you for your help!! :D

Connectez-vous pour commenter.

Plus de réponses (1)

Evan
Evan le 8 Août 2013

0 votes

Here's how you can get rid of the nested loops:
a = [];
for i = 1:2
x(1:5) = i;
y(1:5) = (1:5) + 1;
t = [x; y]';
t(1:5,1) = t(1:5,1) + (1:5)';
t(1:5,2) = t(1:5,2) + 2*(1:5)';
a = [a; t];
end
disp(a)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by