BASIC COMMANDS IN MATLAB
| BASIC COMMANDS IN MATLAB |
CONTENTS:
- AIM
- INTRODUCTION
- VARIOUS METHODS & EXPLANATION
- FINAL CODE
- OUTPUT
AIM:
To create a row vector and a column vector in MATLAB using the following operators and commands:
- Concatenation operator
- Range operator
- Linspace command
- Rand command
INTRODUCTION:
There are three distinct ideas about vectors. The physics student perspective, the computer science student perspective and the mathematician’s perspective.
- For a physics student, a vector can be defined as a physical quantity which has magnitude (length of an arrow) as well as a direction (head of an arrow) associated with it.
- For a computer science student, a vector is just an orderly fashioned number of lists. For example, a list of number representing the price and the square feet area for a property say, house = [50 800]. It is important to note that the order matters here such that 50 would represent the price in lakhs i.e., 50 lakhs. and 800 will correspond to an area of 800 square feet.
- A mathematician tries to generalise both these views of a physics student and computer science student and proceeds to simple and complex matric algebra.
In MATLAB, we will treat a vector as a one-dimensional array which contains number of elements arranged row-wise or column-wise. It is called as a row vector if elements are arranged row-wise or called as a column vector if elements are arranged column-wise. 1xN is the size of a row vector of one row and N columns. Mx1 is the size of a column vector of M rows and one column.
VARIOUS METHODS, THEIR SYNTAX, EXAMPLES & EXPLANATION TO CREATE A VECTOR:
1. Using Concatenation Operator:
A row or a column vector can be created using square brackets [ ] (i.e. the concatenation operator). Elements of the same row are separated by blank space or comma. Different rows of the matrix are separated by a semicolon.
Example:
Rupesh_RV1 = [10 12 13 16 19]
Rupesh_CV1 = [11; 13; 15; 16; 19]
Here, Rupesh_RV1 represents a row vector and Rupesh_CV1 represents a column vector. The outputs for these codes can be seen in the output section of this report.
2. Using Range Operator:
A row vector with evenly spaced elements can be created by the following command:
Syntax: variable_name = k : l : m
Where, k is the initial value, l is the increment and m is the final value.
Example:
Rupesh_RV2 = 18:2:29
Rupesh_CV2 = [22:2:31]'
Rupesh_RV2 represents a row vector. The initial value, 18 is incremented by 2 and so the end value of the row vector is 28 rather than 29. Because after 28 the next number would be 30, but it is not within the range so it’s not printed.
Note that for creating the column vector using range operator, a transpose operator (') is used as shown in Rupesh_CV2.
3. Using Linspace Command:
Syntax: variable_name = linspace (a, b, n)
It generates 'n' equally spaced elements between numbers a and b.
Example:
Rupesh_RV3 = linspace (10,19,5)
Rupesh_CV3 = linspace (12,19,5)'
Similar to the case in a column vector generation using range operator here too the transpose operator (') is used as shown in Rupesh_CV3.
4. Using Rand Command:
Syntax: variable_name = rand (a, b)
It generates a matrix with ‘a’ rows and ‘b’ columns witch each value being a random number between 0 and 1.
Example:
Rupesh_RV4 = rand (1,5)
Rupesh_CV4 = rand (5,1)
Note that (1,5) is used so as to get a matrix with one row and 5 columns (i.e. a row vector) with 5 random numbers between 0 and 1.
FINAL CODE:
OUTPUT: