Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Functions to Create

Matrices

a=
1

13

>> v=[1,2,3,4];
>> a=diag(v)

>> A=[4 3 2; 8 7 6 ; 5 4 3];

a=
1

>> m=diag(A)

m=
4
7

Use Diag to Extract Diagonal


>> b=[1:5;6:10; 11:15]

Subscript Notation

b=
1

10

11

12

13

14

15

Used To refer an element of


matrix:
>> A=[1 2 3; 4 5 6; 7 8 9];
>> b=A(3,3)

>> w=diag(b)

b=

w=

1
7

>> c=A(2,3)

13

c=
6

Creating Matix With Specified


Diagonal
>> a=diag(w)

>> d=A(1,1)
d=
1

Assign matrix element

Reffering to element beyond the


dimension

>> A(1,1)=d/b
>> A(4,4)=12

A=

A=

0.1111

2.0000

3.0000

4.0000

5.0000

6.0000

7.0000

8.0000

9.0000

12

>> A(3,3)=b/c
A=
0.1111

2.0000

3.0000

>> A(4,1)=1;A(4,2)=1;A(4,3)=1

4.0000

5.0000

6.0000

A=

7.0000

8.0000

1.5000

>> A=A(1,4)

??? Index exceeds matrix dimensions

12

>> t=1:2:8
t=

>> A(1,1)=1

A=

1.0000

2.0000

3.0000

4.0000

5.0000

6.0000

>> t=0:2:8

7.0000

8.0000

1.5000

t=
0

>> A(1,1)=5; A(3,3)=9


A=
5

>> x=0.0:0.5:2

x=

0.5000

>> x=(1:6)'

1.0000

1.5000

2.0000

>> A=ones(8,8);
x=

>> A(3:6,3:6)=zeros
1

A=

>> A=[1 2 3; 4 5 6; 7 8 9];

>> A(:,1)

ans =
1
4
7

>> A(2,:)
ans =
4

>> A(2:3,1)
ans =
4
7

>> A(1:2,2:3)
ans =
2

You might also like