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

Click For Summary

Discussion Overview

The discussion revolves around the use of a FOR loop in MATLAB when two variables, m and q, are changing. Participants explore the syntax and logic required to implement such a loop, addressing issues related to variable dependencies and MATLAB's loop structure.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant questions the necessity of including variable m in the loop, noting it is not utilized within that specific context.
  • Another suggests using separate loops for independent variables or making one dependent on the other, providing an example of how to structure the loops based on variable relationships.
  • A participant asks for clarification on what should happen if m and numfeature differ, indicating a potential issue with variable alignment.
  • Several participants discuss the syntax of MATLAB's FOR loop, specifically the use of variables in the initialization, step size, and maximum value, with some expressing uncertainty about the correct usage.
  • There is a consensus among some participants that the syntax attempted by the original poster is incorrect, particularly regarding the inclusion of an assignment within the loop declaration.

Areas of Agreement / Disagreement

Participants express differing views on the correct implementation of the FOR loop syntax in MATLAB, with no consensus reached on the best approach to handle the changing variables m and q.

Contextual Notes

Limitations include unresolved questions about the relationship between m and numfeature, and the specific requirements for the loop's functionality, which may affect the proposed solutions.

jiaying
Messages
1
Reaction score
0
Hey,for following code the 3rd loops i need to have 2 variables m and q .Could anyone help?
Matlab:
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:
Physics news on Phys.org
I assume you're asking about this loop.
Matlab:
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.
 
  • Like
Likes   Reactions: jedishrfu
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:
What do you want to happen if m and numfeature are not the same value?
 
It helps if you say in words what you want to do. In MATLAB the syntax for for loop is

Code:
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?
 
S_David said:
I don't think you can include a variable in the stepSize option.
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).
 
I think you can use variables in the all three, too, but I don't think you can use the following syntax

Code:
initialValue=1;
endValue=10;
for ii=initialValue:m=0.5:endValue

as the OP tried to do.
 
Last edited:
S_David said:
I think you can use variables in the all three, too, but I don't think you can use the following syntax

Code:
initialValue=1;
maxValue=10;
for ii=initialValue:m=0.5:endValue

as the OP tried to do.
The problem is the "m=" part.
I believe this would be fine:
Matlab:
initVal=1;
stepVal = 0.5
endVal=10;
for ii=initVal : stepVal : endVal
;; loop body
end
 
  • Like
Likes   Reactions: EngWiPy

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K