Saturday, April 18, 2020

MATLAB Basics Part 2

1. What is the use of 'hold on' while plotting ?
'hold on' command is used for plotting two or more graphs on a single graph of difference function.

2.How to draw Multiple Plots ? How to create legend for multiple plots?
Script :
>>month = 1:1:12;
>>maxTemp= linspace(15,50,12);
>>avgTemp= linspace(10,30,12);
>>p1 = bar(month,maxTemp,'yellow');
>>hold on;
>>p2 = plot(month,avgTemp,'red');
>>xlabel('Months');
>>hold off;
>>ylabel('Temprature');
>>title('Town Temparature')
>>legend([p1,p2],{'maxTemp','avgTemp'})



legend syntax for multiple plots :
>>legend([p1,p2],{'maxTemp','avgTemp'})




3.How to access multiple elements of matrix at a time ?
Script :
data = [18,32,26,28,46;25,42,35,30,52;
        43,44,37,52,54;49,38,59,54,55;
        55,48,61,69,62;48,34,56,42,56];
rows = [1,5];
cols = 2:4;
subdata = data(rows,cols);%{Fetches the data only part of the given selection}%





4.Mean of Matrix ? Example ?
Mean = (sum of all elements )/(total elements) ;

Mean Of Matrix :
1.If A is a vector, then mean(A) returns the mean of the elements.
2.If A is a matrix, then mean(A) returns a row vector containing the mean of each column.
3.If M = mean(A,'all') computes the mean over all elements of A
4.If M = mean(A,dim) returns the mean along dimension dim. For example, if A is a matrix, then mean(A,2) is a column vector containing the mean of each row.

Script :

A = [0 1 1;
     2 3 2;
     1 3 2;
     4 2 2];
M = mean(A)
M = mean(A,2)
M = mean(A,'all')


5.Built in functions for Matrix Creation ,Name few?


Function Name
Use
eye
Identity Matrix
zeros
Matrix of all 0's
random
Normally disturbed random numbers
diag
Diagonal Matrix
Ones
Matrix of all 1's
rand
Uniformly distributed random numbers
randi
Uniformly distributed random integers

Script :
>>I= eye(5);
>>Z = zeros(5);
>>ZM = zeros(5,3);
>>Rn = randn(3,3);
>>D = diag(3,3);
>>O = ones(4,4);
>>R = rand(2,2);
>>R1 = randi(2,2);



6.Determine Array Size / Length ?
length  function for --> Vectors
size function for --> Matrix
>>I = eye(5)
>>size(I)
>>v =[1,2,3,4];
>>length(v)



7.Reshaping the arrays ?
Script :
v = [8 3 4 1 5 9 6 7 2 2 1 5];
Rex = reshape(v,3,4)
Rex2 = reshape(v,4,3)
Rex3 = reshape(v,6,[])
v2 = Rex3(:)

No comments:

Post a Comment