Basics of MATLAB | Chapter 2

Playing with Array

Now some functions of Array
x=x'                    Transpose
x=[sum(x)]         Sum of elements
length(x)             Length of matrix or calculating dimensions
numel(x)             Determining total elements on matrix or array
sort(x,'ascend')    Ascending order  of elements array
sort(x,'descend')  Descending order  of elements array
mean(x)               Getting mean
max(x)                 Finding largest no. in series
min(x)                  Finding smallest no. in series

Addressing of array in MATLAB

Vector
Let us suppose array
 >>x=9, 8,6,5,1
Then we have to put any element in variable y let us say we have to put 6, 5 in y then,
>>y=x (3, 4) where 3, 4 is address of array
>>y=6 5 we will get output

Changing an element b/w array
let >>x=9, 8,6,5,1
now we have to change element no. 6 to 10, then we see that 6 is on x(3) so therefore 
>>x(3)=10
>>x=9, 8,10,5,1


This is about addressing a vector, Try this in your MATLAB and you will see it there.


Matrix
Here are some Function on addressing of matrix elements.
X=Matrix
m- 1st Row element
n- 1st Columns element
p- Any element on Row
q- Any element on Column

Then X(m,n) Shows 1st row & 1st column element
>>X(m,[n1 n2 n3] )  Addresses 1st  element row & given column
>>X([m1 m2 m3 ],n) Addresses given rows and 1st element Column
>>X([m1,m2],[n1,n2]) specified rows and column
>>X(m,n:q) 1st element row & column (n to q) all
>>X(m:p,n) 1st element Column & row (m to p) all
>>X(: , n:q) Addresses all row & column (n to q) all
>>X(m:p , :) Addresses all column & row (m to p) all
>>X(: , [n1 n2 n3] )  Addresses all row & specified column

>>X([m1 m2 m3 ] , :)  Addresses all column & specified row
>>diag(x) Getting diagonal of Matrix


Concatenate array function.
let us say there are two array and two matrix a & b
>>cat(1,a,b)
Remember
  • 1 is for vertical shift of array or matrix
  • 2 is for horizontal shift of array or matrix
  • 3 is for separation of array or matrix
Kindly apply these commands in your matlab until and unless they are of no use.


Matrix property in MATLAB

Let us say there are two matrix X & Y

Addition of two matrix is >>Z=X+Y
Subtraction  of two matrix is >>Z=X-Y
Multiplication  of two matrix is >>Z=X*Y

Squaring of matrix  >>Z^2 , will get square of given matrix
Cube of matrix   >>Z^3 , will get cube of given matrix

>>sum(x,1) Addition of all column element
>>sum(x,2) Addition of all row element
>>prod(x,1) Product of all column element
>>prod(x,2) Product of all row element
>> fliplr(x)  Flipping left to right
>> flipud(x) Flipping up and down


Try these function on your MATLAB

Comments