MATLAB – Векторы
Векторы строк создаются путем заключения набора элементов в квадратных скобках с использованием пробела или запятой для разделения элементов.
MATLAB выполнит приведенный выше оператор и вернет следующий результат –
Векторы столбцов
Векторы столбцов создаются заключением набора элементов в квадратные скобки с использованием точки с запятой для разделения элементов.
MATLAB выполнит приведенный выше оператор и вернет следующий результат –
Ссылка на элементы вектора
Вы можете ссылаться на один или несколько элементов вектора несколькими способами. I- й компонент вектора v обозначается как v (i). Например –
MATLAB выполнит приведенный выше оператор и вернет следующий результат –
Когда вы ссылаетесь на вектор с двоеточием, например, v (:), в нем отображаются все компоненты вектора.
MATLAB выполнит приведенный выше оператор и вернет следующий результат –
MATLAB позволяет вам выбрать диапазон элементов из вектора.
Например, давайте создадим вектор-строку rv из 9 элементов, затем мы будем ссылаться на элементы с 3 по 7, написав rv (3: 7), и создадим новый вектор с именем sub_rv .
MATLAB выполнит приведенный выше оператор и вернет следующий результат –
Creating Vectors in Matlab; Different Methods and Examples
This text shows how to obtain vectors in Matlab and how to create vector variables in Matlab codes. As its name implies, Matlab generally works with the base of matrices. Also, vectors are matrices and you could do various matrice calculations with your created vector in Matlab. Also, you could do mathematical calculations with Matlab® vectors and you could insert the Matlab® vector into mathematical calculations with other matrices, vectors, and individual variables. Moreover, this is much easier in Matlab® coding compared with other software languages. And you will see creating vectors in Matlab is very simple.
Table of Contents ;
How is The Creation of Vectors in Matlab®?
Firstly, there are a bunch of vector examples in the Matlab® script above, we will talk about their working principles below.
As you can see the v vector is very basic and shows how to create a vector in Matlab in principle. d vector shows how to create column vectors in Matlab®. You could understand that if you put ‘;’ between vector numbers, this vector will be a column vector like d. The meaning of z vector, ve create the z vector starts with 1, ends with 3, and increment of each element in z vector is 0.5. You could type the first value, increment value, and end value to create a vector by putting ‘:’ between these values.
Finally in p vector, we did a bunch of mathematical calculations by using a vector-like z. As you can see we multiplied an increment vector with pi and divided it with 4.
Examples About Creating Vectors in Matlab
Transpose of a Vector
Matlab provides various kinds of tools to create vectors of different types. For example, we create a vector in which the Matlab calculated its transpose of it. To calculate a transpose of a vector, just add ‘ to the end of the vector.
Look at the example above. We created a vector that has 6 elements inside it. To calculate the transpose of it, we added a quote at the end of the square brackets. So, if you type the name of the vector, you will see the transposition of it on the screen like above.
Incremental Vector
You can also define incremental vectors in Matlab. Inside the square brackets above, you just need to type the first and last numbers by putting a semicolon between them. By doing it, you define a vector that has all the values between the values that you enter.
Check the example above. We created a vector that has the first value of 1 and the last value of 6. And if we call this vector again, you can see all the values between 1 and 6 are the separate elements of that vector.
This is another method to create incremental vectors in Matlab. Respectively, you type the first value of the vector and you type the increment value. And you type the last value of the vector. If you call this vector, the elements of the vector is created with the elements of this incremental value.
Calling the Elements of a Vector in Matlab
If you create a vector in Matlab, you can call the separate elements of that vector. You can use these separate elements in different calculations.
For example, we created a vector with an incremental value. If we need to call the third value of that vector, we can write t(3). This will call that element of that vector.
Substractions and Additions of Vectors in Matlab
You can make different substractions and additions to the vectors that you create in Matlab. Check the examples below.
As you see above, we can simply add or subtract the vectors in Matlab easily. All the elements are substracted and added between themselves in Matlab. You can use this feature of Matlab in different calculations.
Multiplications and Divisions of Vectors
We can also multiply and divide the vectors that we create in Matlab with different numbers. You can check the code examples below.
As you see in the example above, we multiplied and divided the vector that we create in Matlab. You can see the replies of the Matlab program on these mathematical calculations.
Above all, besides the creating vector in Matlab, there are various kinds of tools to create and manipulate the vectors to make effective calculations. Check the related articles about vectors in Matlab;
Conclusion
As you see above, there are lots of vector-creating methods and commands in the Matlab programming language. You can make different and complex calculations with the vectors that you create.
So, do not forget to leave your comments and questions below about creating vectors in Matlab below.
If you want further coding examples about ‘the vector creation in Matlab®, inform us in the comments.
This article is prepared for completely educative and informative purposes. Images used courtesy of Matlab®
How to initialize a vector in MATLAB as per a pattern?
I am completely new to MATLAB.This may be a rather basic question.
Given numerical values for size , extras and max , I need to initialize a 1 X N vector such that the first size elements are 1, the next size are 2, the next size are 3 and so on till the last size elements are set to max . So I need to initialize size number of elements successively to x such that x increments from 1 to max . The extras are the number of leftover cells which are initialized to 0. To illustrate:
Introduction to Vectors in Matlab¶
This is the basic introduction to Matlab. Creation of vectors is included with a few basic operations. Topics include the following:
Defining a Vector¶
Matlab is a software package that makes it easier for you to enter matrices and vectors, and manipulate them. The interface follows a language that is designed to look a lot like the notation use in linear algebra. In the following tutorial, we will discuss some of the basics of working with vectors.
If you are running windows or Mac OSX, you can start matlab by choosing it from the menu. To start matlab on a unix system, open up a unix shell and type the command to start the software: matlab. This will start up the software, and it will wait for you to enter your commands. In the text that follows, any line that starts with two greater than signs (>>) is used to denote the matlab command line. This is where you enter your commands.
Almost all of Matlab’s basic commands revolve around the use of vectors. A vector is defined by placing a sequence of numbers within square braces:
This creates a row vector which has the label “v”. The first entry in the vector is a 3 and the second entry is a 1. Note that matlab printed out a copy of the vector after you hit the enter key. If you do not want to print out the result put a semi-colon at the end of the line:
If you want to view the vector just type its label:
You can define a vector of any size in this manner:
Notice, though, that this always creates a row vector. If you want to create a column vector you need to take the transpose of a row vector. The transpose is defined using an apostrophe (‘):
A common task is to create a large vector with numbers that fit a repetitive pattern. Matlab can define a set of numbers with a common increment using colons. For example, to define a vector whose first entry is 1, the second entry is 2, the third is 3, and sequentially through 8, you enter the following:
If you wish to use an increment other than one that you have to define the start number, the value of the increment, and the last number. For example, to define a vector that starts with 2 and ends in 4 with steps of 0.25 you enter the following:
Accessing elements within a vector¶
You can view individual entries in this vector. For example to view the first entry just type in the following:
This command prints out entry 1 in the vector. Also notice that a new variable called ans has been created. Any time you perform an action that does not include an assignment matlab will put the label ans on the result.
To simplify the creation of large vectors, you can define a vector by specifying the first entry, an increment, and the last entry. Matlab will automatically figure out how many entries you need and their values. For example, to create a vector whose entries are 0, 2, 4, 6, and 8, you can type in the following line:
Matlab also keeps track of the last result. In the previous example, a variable ans is created. To look at the transpose of the previous result, enter the following:
To be able to keep track of the vectors you create, you can give them names. For example, a row vector v can be created:
Note that in the previous example, if you end the line with a semi- colon, the result is not displayed. This will come in handy later when you want to use Matlab to work with very large systems of equations.
Matlab will allow you to look at specific parts of the vector. If you want to only look at the first three entries in a vector you can use the same notation you used to create the vector:
Basic operations on vectors¶
Once you master the notation you are free to perform other operations:
For the most part Matlab follows the standard notation used in linear algebra. We will see later that there are some extensions to make some operations easier. For now, though, both addition subtraction are defined in the standard way. For example, to define a new vector with the numbers from 0 to -4 in steps of -1 we do the following:
We can now add u and v together in the standard way:
Additionally, scalar multiplication is defined in the standard way. Also note that scalar division is defined in a way that is consistent with scalar multiplication:
With these definitions linear combinations of vectors can be easily defined and the basic operations combined:
You will need to be careful. These operations can only be carried out when the dimensions of the vectors allow it. You will likely get used to seeing the following error message which follows from adding two vectors whose dimensions are different: