# Using for loop in matlab

6 views (last 30 days)
Aftab Ahmed Khan on 2 Feb 2015
Edited: Matt J on 2 Feb 2015
Hi everyone, I am assigning these values not in a very efficient way, can you suggest me how can i do it more efficiently? Thank you so much.
%Zone 1
absx(1,81)=0;
absy(1,81)=0;
absx(1,82)=0;
absy(1,82)=0;
absx(1,83)=450;
absy(1,83)=0;
absx(1,84)=450;
absy(1,84)=0;
absx(1,85)=450;
absy(1,85)=450;
absx(1,86)=450;
absy(1,86)=450;
absx(1,87)=0;
absy(1,87)=450;
absx(1,88)=0;
absy(1,88)=450;
%Zone 2
absx(1,89)=0;
absy(1,89)=900;
absx(1,90)=0;
absy(1,90)=900;
absx(1,91)=450;
absy(1,91)=900;
absx(1,92)=450;
absy(1,92)=900;
absx(1,93)=450;
absy(1,93)=1350;
absx(1,94)=450;
absy(1,94)=1350;
absx(1,95)=0;
absy(1,95)=1350;
absx(1,96)=0;
absy(1,96)=1350;
%Zone 3
absx(1,97)=900;
absy(1,97)=0;
absx(1,98)=900;
absy(1,98)=0;
absx(1,99)=900+450;
absy(1,99)=0;
absx(1,100)=900+450;
absy(1,100)=0;
absx(1,101)=900+450;
absy(1,101)=450;
absx(1,102)=900+450;
absy(1,102)=450;
absx(1,103)=900;
absy(1,103)=450;
absx(1,104)=900;
absy(1,104)=450;
%Zone 4
absx(1,105)=900;
absy(1,105)=900;
absx(1,106)=900;
absy(1,106)=900;
absx(1,107)=900+450;
absy(1,107)=900;
absx(1,108)=900+450;
absy(1,108)=900;
absx(1,109)=900+450;
absy(1,109)=900+450;
absx(1,110)=900+450;
absy(1,110)=900+450;
absx(1,111)=900;
absy(1,111)=1350;
absx(1,112)=900;
absy(1,112)=1350;

Andreas Goser on 2 Feb 2015
Why do you think it is not efficient?
Aftab Ahmed Khan on 2 Feb 2015
Well, its like 60 lines of code for doing a simple task. I am sure it can be done more efficently in a for loop.

Matt J on 2 Feb 2015
Edited: Matt J on 2 Feb 2015
You would do things like the following
absx=zeros(1,112);
absy=zeros(1,112);
%Zone 1
absx(83:86)=450;
absy(85:88)=450
and similarly for the other Zones.