Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab -- how can i use a FOR loop if I have 2 variables changing

Tags:
  1. Oct 12, 2016 #1
    Hey,for following code the 3rd loops i need to have 2 variables m and q .Could anyone help?
    Code (Matlab M):
    for t=1:maxiterations
        toterror=0;
        for j=1:numfeature
            totslope=0;
            for i=1:m&&q=1:numfeature
                  z=0;
               for jj=1:numfeature
                   z=z+prevtheta(jj)*x(i,jj);
               end
               h=1.0/(1.0+exp(-z));
               H=H+(x(i,q)*h*(1-h)*x(i,jj));
             
               totslope=(totslope+(h-y(i))*x(i,j));
               toterror=(toterror+-y(i)*log(h)-(1-y(i))*log(1-h));        
            end      
       toterror=toterror/numtrainsam;
       theta(j)= theta(j)-H\totslope;
        end
        prevtheta=theta;
          errorperiteration(t)=toterror/j;
    end
     
    Last edited by a moderator: Oct 12, 2016
  2. jcsd
  3. Oct 12, 2016 #2

    Mark44

    Staff: Mentor

    I assume you're asking about this loop.
    Code (Matlab M):
    for i=1:m&&q=1:numfeature
                  z=0;
               for jj=1:numfeature
                   z=z+prevtheta(jj)*x(i,jj);
               end
               h=1.0/(1.0+exp(-z));
               H=H+(x(i,q)*h*(1-h)*x(i,jj));
             
               totslope=(totslope+(h-y(i))*x(i,j));
               toterror=(toterror+-y(i)*log(h)-(1-y(i))*log(1-h));        
            end
    Why is m in there? It's not used in that for loop, and it's not used outside the loop.
     
  4. Oct 12, 2016 #3

    jedishrfu

    User Avatar

    Staff: Mentor

    In general, you would either use separate loops if they vary independently or make one dependent on the other.

    Say m varies from 1 to 10 and n varies from 4 to 40

    Then loop on m and compute n = 4*m inside the loop.
     
    Last edited: Oct 12, 2016
  5. Oct 12, 2016 #4

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    What do you want to happen if m and numfeature are not the same value?
     
  6. Nov 16, 2016 #5
    It helps if you say in words what you want to do. In MATLAB the syntax for for loop is

    Code (Text):
    for ii=intialValue: stepSize: maxValue
    I don't think you can include a variable in the stepSize option. What do you want to do with this for loop?
     
  7. Nov 17, 2016 #6

    Mark44

    Staff: Mentor

    I'm fairly certain that you can use variables for all three: initVal, stepVal, and endVal. I don't have matlab, so I can't confirm this.

    However, the for loop body can't change the value of the index variable (the loop control variable).
     
  8. Nov 17, 2016 #7
    I think you can use variables in the all three, too, but I don't think you can use the following syntax

    Code (Text):
    initialValue=1;
    endValue=10;
    for ii=initialValue:m=0.5:endValue
    as the OP tried to do.
     
    Last edited: Nov 17, 2016
  9. Nov 17, 2016 #8

    Mark44

    Staff: Mentor

    The problem is the "m=" part.
    I believe this would be fine:
    Code (Matlab M):

    initVal=1;
    stepVal = 0.5
    endVal=10;
    for ii=initVal : stepVal : endVal
    ;; loop body
    end
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted