Use results of a 'for' loop to formulate a vector

In summary, a 'for' loop can be used to iterate through a set of data and perform a specific action on each element. By utilizing the results of the loop, a vector can be formulated to store the data in a structured and organized manner. This can be useful for analyzing and manipulating large datasets efficiently.
  • #1
timsea81
89
1
I would think this should be an easy one...

For i=0 to i=M, I want to run a a calculation and get M different results. I want to store those M results as a vector so I can plot them.

Simplify version below:

for i=0:10
T = i/2;
T
end

This spits out a list of numbers: 0, 0.5, 1... How do I store that list as a vector and plot it?
 
Physics news on Phys.org
  • #2
It depends what language you are using as there's differences between them and how they handle vectors and what graphing options they have.
 
  • #3
trollcast said:
It depends what language you are using as there's differences between them and how they handle vectors and what graphing options they have.

I think I got it now:

T1 = [1:5];
for i=1:5,
T1(i) = i^6;
T2(i) = i^.01;
plot (T1)
end

Now I just need to figure out how to plot two vectors on top of each other in different colors, but I think I should be able to get that part from the help. Thanks for the response.
 
  • #5

Related to Use results of a 'for' loop to formulate a vector

1. What is a 'for' loop in programming?

A 'for' loop is a control flow statement that allows for the execution of a specific block of code for a predetermined number of times. It is commonly used in programming to iterate through a collection of data or perform a repetitive task.

2. How can the results of a 'for' loop be used to formulate a vector?

The results of a 'for' loop can be used to populate a vector by assigning each iteration's output to a specific index in the vector. This allows for the storage and organization of data in a structured manner.

3. What are the advantages of using a 'for' loop to formulate a vector?

Using a 'for' loop to formulate a vector allows for the automation of the process, making it easier to handle larger datasets. It also ensures that each element of the vector is accurately assigned and reduces the potential for human error.

4. Can a 'for' loop be used to create a vector of different data types?

Yes, a 'for' loop can be used to create a vector of different data types. This can be achieved by using an if/else statement within the loop to check the data type of each iteration's output and assign it to the appropriate index in the vector.

5. Are there any limitations to using a 'for' loop to formulate a vector?

One limitation of using a 'for' loop to formulate a vector is that it requires a predetermined number of iterations, which may not always be known. In such cases, alternative methods such as a 'while' loop may be more suitable.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
291
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
594
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
245
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
29
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
858
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top