How to Create a Simple Vector in MATLAB?

  • Thread starter sara_87
  • Start date
  • Tags
    Matlab
In summary, to produce a vector t(i) where t(i)=i and i=1:10, you can either use a for loop and clear the variable t first, or use vectorized code t = 1:10. This will give the desired output of 1,2,3,4,5,6,7,8,9,10 without any additional elements.
  • #1
sara_87
763
0

Homework Statement



i want to produce a vector t(i) where t(i)=i and i=1:10

Homework Equations





The Attempt at a Solution



for i=1:10
t(i)=i
end

this gives:
1,2,3,4,5,6,7,8,9,10,10

why is it giving me 11 elements? with two 10's?
i just want:
1,2,3,4,5,6,7,8,9,10
 
Physics news on Phys.org
  • #2
It's possible t was already defined so that even though that part of your script should work, it is not changing the value of t(11). Try clearing the value first:

clear t
for i = 1:10
t(i) = i
end

Alternatively, you can use vectorized code:

t = 1:10;

This is cleaner, takes advantage of what MATLAB can do, and wouldn't require you to clear the variable first.
 
  • #3
thanks, that works for me.
 

Related to How to Create a Simple Vector in MATLAB?

1. What is MATLAB and how is it used?

MATLAB is a programming language and interactive computing environment commonly used for scientific and engineering applications. It allows for easy and efficient data analysis, visualization, and algorithm development.

2. How do I perform basic calculations in MATLAB?

To perform basic calculations in MATLAB, you can use the command line to enter mathematical expressions. For example, typing "2+3" and pressing enter will output the result of 5. You can also use built-in functions such as "sin", "cos", "sqrt", etc. to perform more complex calculations.

3. How do I plot data in MATLAB?

To plot data in MATLAB, you can use the "plot" function. This function takes in two vectors, one for the x-values and one for the y-values, and creates a plot with those data points. You can also add labels, titles, and customize the appearance of the plot using various commands.

4. How do I create and manipulate arrays in MATLAB?

Arrays are a fundamental data structure in MATLAB and can be created using the square brackets notation. For example, typing "[1,2,3]" will create a 1x3 array with the values 1, 2, and 3. You can manipulate arrays by indexing and slicing them, performing mathematical operations, and using built-in functions.

5. Can I use MATLAB for machine learning and data analysis?

Yes, MATLAB has various toolboxes and functions specifically designed for machine learning and data analysis tasks. These include statistical analysis, neural networks, and machine learning algorithms. MATLAB also has the ability to import and manipulate large datasets, making it a powerful tool for data analysis.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
952
  • Engineering and Comp Sci Homework Help
Replies
2
Views
842
  • Engineering and Comp Sci Homework Help
Replies
3
Views
820
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
20
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top