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

In summary, the conversation discusses using a for loop in MATLAB and the possibility of using variables in the step size option. The loop in question includes a variable "m" that is not used in the loop and it is unclear how the loop should behave if the values of "m" and "numfeature" are not the same. It is also mentioned that variables can be used in all three parts of the for loop, but the syntax used in the conversation may not be valid.
  • #1
jiaying
1
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
  • #2
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 jedishrfu
  • #3
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:
  • #4
What do you want to happen if m and numfeature are not the same value?
 
  • #5
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?
 
  • #6
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).
 
  • #7
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:
  • #8
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 EngWiPy

1. How do I set up a FOR loop with two changing variables in Matlab?

In order to use a FOR loop with two variables in Matlab, you can use nested FOR loops. The inner loop will control the first variable, while the outer loop controls the second variable. For example:

for i = 1:10

    for j = 1:5

        disp([i,j]);

    end

end

This will print out all combinations of i and j from 1 to 10 and 1 to 5, respectively.

2. Can I use a FOR loop to iterate through two arrays simultaneously in Matlab?

Yes, you can use a FOR loop to iterate through two arrays simultaneously by using the "length" function to determine the length of the arrays, and then using the index of the FOR loop to access the elements of both arrays. For example:

a = [1,2,3];

b = [4,5,6];

for i = 1:length(a)

    disp([a(i), b(i)]);

end

This will print out pairs of elements from a and b, such as [1,4], [2,5], [3,6].

3. How can I use a FOR loop to perform a task a specific number of times in Matlab?

You can use the "colon" operator to specify the range of values to iterate through in the FOR loop. For example, if you want to perform a task 5 times, you can use:

for i = 1:5

    disp("Performing task");

end

This will execute the task 5 times.

4. Is it possible to use a FOR loop to iterate through a matrix in Matlab?

Yes, you can use a FOR loop to iterate through a matrix in Matlab by using the "size" function to determine the number of rows and columns in the matrix, and then using nested FOR loops to iterate through each element. For example:

A = [1,2,3; 4,5,6; 7,8,9];

for i = 1:size(A,1)

    for j = 1:size(A,2)

        disp(A(i,j));

    end

end

This will print out each element in the matrix A.

5. Can I use a FOR loop to iterate through a cell array in Matlab?

Yes, you can use a FOR loop to iterate through a cell array in Matlab by using the "length" or "size" function to determine the number of elements in the cell array, and then using the index of the FOR loop to access the elements. For example:

C = {'apple','banana','orange'};

for i = 1:length(C)

    disp(C{i});

end

This will print out the elements in the cell array C, such as 'apple', 'banana', 'orange'.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
575
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top