Variables
MATLAB is based on matrix, even that a scalar quantity suppose (x=1) is Fitted in 1x1 matrices.
Variable are given by simple command like >>x=1
This creates a variable x and assign the value '1' to it, assigned value is stored in Workspace as you can see in Fig given below.
Variable Rules:
- Variable should be start with Character Alphabet, it should not be any number.
- There should not be any space between variable name.
- Variable name should not be any predefined function.
Here are some predefined functions that can be used when defining a variable.
rand %Generates uniformly distributed random numbers between 0 and 1.
randn %Generates normally distributed random numbers.
complex % For Complex variables
exp(x) %Exponential
log(x) %Natural logarithm.
ln(x) %log10(x) Common (base 10).
sqrt(x) %Square root of x.
Array
Definition- Array is a data structure consist of collection of elements, values and variables.
In MATLAB Arrays are classified in Vector & Matrix
Here are two things we have to work on array these are
- Creating Array- It means creating a array one dimensional and multidimensional.
- Addressing Array- It means to address particular element in array how to address elements in array by Row and Column.
Vector are divided into three types.
- Random Vector
- Series Vector
- Linear Vector
Random Vector
- Use always Square Brackets or Closed Bracket "[ ]"
- We can separate elements by Comma '','' & Space " "
For example- >>x=[1,2,3] OR >>x=[1 2 3]
Series Vector
- Series vector is represented by [m:n], where m is First No. & n is Nth Number.
- If we use colon then square bracket are optional otherwise compulsory.
- Suppose common difference is missed then MATLAB will automatically take ones common difference For example-
>>x[1:5]
>>x[1 2 3 4 5]
- In case last no. is out of series MATLAB will take previous value by its own, For example-
>>x= [1:2:9]
% then value displayed will be
>>x=[1 2 4 6 8]
%These Values are stored in x]
Linear Vector
function for linear vector is linspace(m,n,q)
where m is 1st element, n is last element, and q is total values b/w them with common differences.
Note: if we did not put q, in then MATLAB will create 100 Parts by its own.
Parenthesis is compulsory.
Separation of elements are only done by comma
Matrix
A Rectangular array consist of numbers, expressions or symbols arranged in Rows & Column.
- Square brackets are compulsory []
Some MATRIX function are as follows-
x=pascal(x)- Pascal Matrix
x=magic(x)- Magic Matrix wit Random Number
x=eye(x)- Identical Matrix
x=zeros(x)- Zeros Matrix
x=ones(x)- Ones Matrix
See Figure-
Different Matrix |
Comments