How to get the last variable of a for loop in MATLAB

In summary, the person is having trouble getting MatLab to output only the last variable of their "for loop." They are seeking help with an example of notation and will continue to look through their old scripts and functions. They may be referring to the last value of their loop control variable and a possible solution involves using a loop control statement in MatLab.
  • #1
KonaGorrila
12
0
Aloha guys,

I am just having trouble getting MatLab to output only the last variable of my "for loop."
If anyone can help all I am looking for is an example of notation.
p.s. I have done this before just not in a while and will continue to look through my old scripts and functions. just not in a while.
Thank you,
HWN Phisics Nerd :P
 
Physics news on Phys.org
  • #2
KonaGorrila said:
Aloha guys,

I am just having trouble getting MatLab to output only the last variable of my "for loop."
If anyone can help all I am looking for is an example of notation.
p.s. I have done this before just not in a while and will continue to look through my old scripts and functions. just not in a while.
Do you mean the last value of your loop control variable?

If so, here's how you would do it in C, which should be fairly close to what it would be in matlab.

C:
int N = 10;
int i;

for (i = 0; i < N + 1; i++)
{
   if ( i == N) printf("Last value is %d", i);
}
 

1. How can I access the last variable of a for loop in MATLAB?

In order to access the last variable of a for loop in MATLAB, you can use the end keyword followed by the number of loops you want to access. For example, if you have a for loop that goes from 1 to 10, you can access the last variable using end-1.

2. Can I store the last variable of a for loop in a variable?

Yes, you can store the last variable of a for loop in a variable by using the end keyword. For example, if you want to store the last variable of a for loop that goes from 1 to 10, you can use last_variable = end-1;.

3. How do I output the last variable of a for loop in MATLAB?

In order to output the last variable of a for loop in MATLAB, you can use the disp() function and pass in the last variable as an argument. For example, if you want to output the last variable of a for loop that goes from 1 to 10, you can use disp(end-1);.

4. Is it possible to access the last variable of a for loop while the loop is still running?

Yes, it is possible to access the last variable of a for loop while the loop is still running by using the last_variable = end-1; syntax. This will allow you to access the last variable at any point during the loop execution.

5. Can I use the last variable of a for loop in a conditional statement?

Yes, you can use the last variable of a for loop in a conditional statement by storing it in a variable and then using that variable in the conditional statement. For example, if you want to use the last variable of a for loop that goes from 1 to 10 in a conditional statement, you can use last_variable = end-1; if (last_variable > 5) { // do something }.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
851
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top