Hi guys, i want to ask something since im still new using matlab..how can i iterate loop from negative variable to its positive variable?..and do you guys know anything about appearing a GIS/google earth into GUI?

1 view (last 30 days)
for xj= 1 : xj
[x1r,x1m] = calX(xj,x1,yj,y1,i1,angle1);
[x2r,x2m] = calX(xj,x2,yj,y2,i2,angle2);
[x3r,x3m] = calX(xj,x3,yj,y3,i3,angle3);
[x4r,x4m] = calX(xj,x4,yj,y4,i4,angle4);
[x5r,x5m] = calX(xj,x5,yj,y5,i5,angle5);
[x6r,x6m] = calX(xj,x6,yj,y6,i6,angle6);
totalXr = calTotalX( x1r, x2r, x3r, x4r, x5r, x6r);
totalXm = calTotalX( x1m, x2m, x3m, x4m, x5m, x6m);
magX = sqrt(((totalXr)^2) + ((totalXm)^2));
thetaX = atand(totalXm/totalXr);
Bjx = convert_meter_to_feet(magX);
[y1r,y1m] = calY(xj,x1,yj,y1,i1,angle1);
[y2r,y2m] = calY(xj,x2,yj,y2,i2,angle2);
[y3r,y3m] = calY(xj,x3,yj,y3,i3,angle3);
[y4r,y4m] = calY(xj,x4,yj,y4,i4,angle4);
[y5r,y5m] = calY(xj,x5,yj,y5,i5,angle5);
[y6r,y6m] = calY(xj,x6,yj,y6,i6,angle6);
totalYr = calTotalY( y1r, y2r, y3r, y4r, y5r, y6r);
totalYm = calTotalY( y1m, y2m, y3m, y4m, y5m, y6m);
magY = sqrt(((totalYr)^2) + ((totalYm)^2));
thetaY = atand(totalYm/totalYr);
Bjy = convert_meter_to_feet(magY);
omega = atand((((Bjx)^2)* sind(2*thetaX) + ((Bjy)^2)* sind(2*thetaY))/(((Bjx)^2)* cosd(2*thetaX) + ((Bjy)^2)* cosd(2*thetaY)));
Bjxmax = omega + thetaX;
Bjymax = omega + thetaY;
Bjxmin = omega + thetaX + 90;
Bjymin = omega + thetaY + 90;
BjMAX = sqrt(((Bjx*cosd(Bjxmax))^2) + ((Bjy*cosd(Bjymax))^2));
BjMIN = sqrt(((Bjx*cosd(Bjxmin))^2) + ((Bjy*cosd(Bjymin))^2));
Bresult = sqrt((BjMAX^2) + (BjMIN^2));
values_of_field(xj) = Bresult;
value_of_xj_neg(xj) = -xj;
value_of_xj_pos(xj) = xj;
end

Accepted Answer

dpb
dpb on 7 Sep 2014
Edited: dpb on 7 Sep 2014
... how can i iterate loop from negative variable to its positive variable?
Many alternatives...
Generate an array of values across the range and iterate over it--
x=linspace(xLo,xHi,nPoints);
for i=1:length(x)
z=yourfunction(x(i));
Or, generate the value on the fly...
x=xLo;
dx=(xHi-xLo)/(nPoints-1);
for i=1:nPoints
x=xLo+(i-1)*dx;
z=yourfunction(x);
Alternatively, to save values into an array when iterating over a floating point vector, you keep an alternative integer index for that purpose--
idx=0; % initialize an index for the array addressing
for xj=-pi:0.05:pi
...
Bresult=some calculations for values of xj
...
idx=idx+1; % increment the array index
values_of_field(idx) = Bresult;
end
The "Matlab way" would be to vectorize the function to avoid the loop, however. Often this is doable w/ the "dot" element-wise operators. Also, it looks like you could probably eliminate many of the duplicated variable names by using arrays or cell arrays or named structures instead and simplify the coding greatly.

More Answers (2)

Image Analyst
Image Analyst on 7 Sep 2014
Sure. Just set the starting and ending values, and the step, which will be negative to go from higher numbers to lower numbers:
for k = startingValue : -1 : endingValue
startingValue can be positive or negative, and the same for endingValue. If starting value >= endingValue, you will enter the loop otherwise the loop will not be entered. For example
for k = 8 : -1 : -27
  2 Comments
dpb
dpb on 7 Sep 2014
Excepting it looks like OP's query had to do with floating point values where often newbies want to do sotoo--
for xj=-pi:0.05:pi
...
Bresult=some calculations for values of xj
...
values_of_field(xj) = Bresult;
end
The last line in the above loop was extracted from OP's code identically...which is why gave the previous result. Actually, for the above I neglected to add the alternative solution
idx=0; % initialize an index for the array addressing
for xj=-pi:0.05:pi
...
Bresult=some calculations for values of xj
...
idx=idx+1; % increment the array index
values_of_field(idx) = Bresult;
end
Image Analyst
Image Analyst on 7 Sep 2014
Yes. I'm sure your answer is the more comprehensive one - I didn't dig into his code as deeply as you. I was just answering the simple question of how you can iterate in the negative direction (backwards), but maybe he wasn't asking that.

Sign in to comment.


syakirin
syakirin on 8 Sep 2014
thanks guys..it really helps me..

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!