Beruflich Dokumente
Kultur Dokumente
Class: Sem:
To display a message
display('Hello. !! This is an introductory session for MATLAB code')
Variable declaration
a=10
b=20
c=[4 2 3]
d=[5;3;6]
e=[4 3 6;5 4 8; 9 8 7]
f=[5 4 7 6 8
5 8 7 4 6
6 4 2 5 9
8 7 6 9 7]
a = 10
b = 20
c = 4 2 3
d =
e =
4 3 6
5 4 8
9 8 7
f =
5 4 7 6 8
5 8 7 4 6
6 4 2 5 9
8 7 6 9 7
Mathematical operation
aintob=a*b
abyb=a/b
cintod=c*d
dintoc=d*c
aintob = 200
abyb = 0.5000
cintod = 44
dintoc =
20 10 15
12 6 9
24 12 18
Matrix operation
einv=inv(e) expo=exp(e)
etran=e' esin=sin(e) eadd=e+10
emul=10*e elemult=e.*e
matmul=e*e sqrmat=e^2
sreleem=e.^2
einv =
expo = 1.0e+03 *
etran =
4 5 9
3 4 8
6 8 7
esin =
-0.7568 0.1411 -0.2794
-0.9589 -0.7568 0.9894
0.4121 0.9894 0.6570
eadd =
14 13 16
15 14 18
19 18 17
emul =
40 30 60
50 40 80
90 80 70
elemult =
16 9 36
25 16 64
81 64 49
matmul =
85 72 90
112 95 118
139 115 167
sqrmat =
85 72 9
112 95 118
139 115 167
sreleem =
16 9 36
25 16 64
81 64 49
Augmentation of matrix
aug1=[e,e]
aug2=[e;e]
aug1 =
4 3 6 4 3 6
5 4 8 5 4 8
9 8 7 9 8 7
aug2 =
4 3 6
5 4 8
9 8 7
4 3 6
5 4 8
9 8 7
frow1 = 5 4 7 6 8
frow3 = 6 4 2 5 9
fcol2 =
4
8
4
7
fcol5 =
8
6
9
7
frow123 =
5 4 7 6 8
5 8 7 4 6
6 4 2 5 9
fcol34 =
7 6
7 4
2 5
6 9
Define a zero, ones, random, magic, identity, size of matrix
zmat=zeros(3,4)
onesmat=ones(4,3)
mg=magic(4)
random1=rand(2)
random2=rand(2,3)
e(4,4)=10
size(e)
zmat =
0 0 0 0
0 0 0 0
0 0 0 0
onesmat =
1 1 1
1 1 1
1 1 1
1 1 1
mg =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
random1 =
0.1576 0.9572
0.9706 0.4854
random2 =
e =
4 3 4 0
5 4 8 0
9 8 7 0
0 0 0 10
ans = 4 4
For decimal places
format long
plong=e*inv(e)
format short
pshort=e*inv(e)
Column 4
0
0
0
1.000000000000000
pshort =
number = 1 2 3 4 5 6 7 8 9 10
nm = 0 10 20 30 40 50 60 70 80 90
even = 0 2 4 6 8 10
odd = 1 3 5 7 9
centi =
24.4444
subplot(2,2,2);plot(x1,y2)
xlabel('angle from 0 to 2pi')
ylabel('sin of angle form 0 to 2pi')
title('cos vs angle')
Matrix Manipulation
Vinod 86 75 72 94 82
Ajay 68 87 78 78 88
Sanjay 76 91 68 82 84
Rakesh 79 74 65 88 76
Karan 94 82 79 89 72
Give input to MATLAB in the form of matrix
AllMarks = [86 75 72 94 82
68 87 78 78 88
76 91 68 82 84
79 74 65 88 76
94 82 79 89 72]
AllMarks =
86 75 72 94 82
68 87 78 78 88
76 91 68 82 84
79 74 65 88 76
94 82 79 89 72
PEFMarks = 94 78 65 74 68
AllMarks =
86 75 72 94 82 94
68 87 78 78 88 78
76 91 68 82 84 65
79 74 65 88 76 74
94 82 79 89 72 68
PEFMarks= AllMarks(:,6)*0.5
AllMarks(:,6)=PEFMarks
PEMarks =
47.0000
39.0000
32.5000
37.0000
34.0000
AllMarks =
Extract 2nd and 3rd student marks for all subjects and
calculate its total and average marks.
ArunMarks=AllMarks(2,:)
ArunTotal=sum(ArunMarks)
ArunAverage=mean(ArunMarks)
ArunTotal =
438.0000
ArunAverage =
73.0000
VinodMarks=AllMarks(3,:)
VinodTotal=sum(VinodMarks)
VinodAverage=mean(VinodMarks)
VinodTotal =
433.5000
VinodAverage =
72.2500
Calculate average marks for all the subjects
AllMean=mean(AllMarks,1)
AllMean =
AllMarks =