0% found this document useful (0 votes)
5 views13 pages

Matrix Operations and Functions in MATLAB

The document contains a series of matrix operations and manipulations using MATLAB syntax. It includes the creation of matrices, addition, and extraction of rows and columns, as well as the use of functions like eye, ones, zeros, rand, magic, and diag. Additionally, it shows examples of errors encountered while attempting to index and create matrices incorrectly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

Matrix Operations and Functions in MATLAB

The document contains a series of matrix operations and manipulations using MATLAB syntax. It includes the creation of matrices, addition, and extraction of rows and columns, as well as the use of functions like eye, ones, zeros, rand, magic, and diag. Additionally, it shows examples of errors encountered while attempting to index and create matrices incorrectly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

>> G=[0,1,2;5,-6,3;10,15,20]

G=

0 1 2

5 -6 3

10 15 20

>> E=[10,5,6;2,0,3;-20,-6,8]

E=

10 5 6

2 0 3

-20 -6 8

>> G+E

ans =

10 6 8

7 -6 6

-10 9 28

>> eye(3)
ans =

1 0 0

0 1 0

0 0 1

>> ones(3,2)

ans =

1 1

1 1

1 1

>> zeros(2)

ans =

0 0

0 0

>> rand(2,3)

ans =
0.8147 0.1270 0.6324

0.9058 0.9134 0.0975

>> magic(6)

ans =

35 1 6 26 19 24

3 32 7 21 23 25

31 9 2 22 27 20

8 28 33 17 10 15

30 5 34 12 14 16

4 36 29 13 18 11

>> A=magic(5)

A=

17 24 1 8 15

23 5 7 14 16

4 6 13 20 22

10 12 19 21 3

11 18 25 2 9
>> A(1,:)

ans =

17 24 1 8 15

>> A(2,:)

ans =

23 5 7 14 16

>> A(3,:)

ans =

4 6 13 20 22

>> A(4,:)

ans =

10 12 19 21 3

>> A(5,:)
ans =

Columns 1 through 4

11 18 25 2

Column 5

>> A(:,1)

ans =

17

23

10

11

>> A(:,2)

ans =
24

12

18

>> A(:,1;2)

A(:,1;2)

Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched

delimiters.

>> A(:,1,2)

Index in position 3 exceeds array bounds. Index must not exceed 1.

>> A(:,1,2)

Index in position 3 exceeds array bounds. Index must not exceed 1.

>> A(1,2)

ans =

24

>> A=(1,2)
A=(1,2)

Error: Unexpected ',' in parentheses. When creating a matrix, use

square brackets [].

>> A(1,2)

ans =

24

>> A(2,3)

ans =

>> A(19,21;25,2)

A(19,21;25,2)

Invalid expression. When calling a function or indexing a variable,

use parentheses. Otherwise, check for mismatched delimiters.

>> A(4,3;4,4;5,3;5,4)

A(4,3;4,4;5,3;5,4)

Invalid expression. When calling a function or indexing a variable,

use parentheses. Otherwise, check for mismatched delimiters.

>> A(4:5,3:4)

ans =

19 21

25 2

>>

>> diag (A)

ans =

17

13

21

>> diag(A,-1)

ans =
23

19

>> diag(A,1)

ans =

24

20

>> Q=(2,3,5;-2,-5,-6;-1,-1,2)

Q=(2,3,5;-2,-5,-6;-1,-1,2)

Error: Unexpected ',' in parentheses. When creating a matrix, use

square brackets [].

>> Q=[2,3,5;-2,-5,-6;-1,-1,2]

Q=
2 3 5

-2 -5 -6

-1 -1 2

>> triu(Q)

ans =

2 3 5

0 -5 -6

0 0 2

>> Q=magic(6)

Q=

35 1 6 26 19 24

3 32 7 21 23 25

31 9 2 22 27 20

8 28 33 17 10 15

30 5 34 12 14 16

4 36 29 13 18 11

>> Z=triu(Q)
Z=

35 1 6 26 19 24

0 32 7 21 23 25

0 0 2 22 27 20

0 0 0 17 10 15

0 0 0 0 14 16

0 0 0 0 0 11

>> sparse(Q)

ans =

6×6 sparse double matrix (36 nonzeros)

(1,1) 35

(2,1) 3

(3,1) 31

(4,1) 8

(5,1) 30

(6,1) 4

(1,2) 1

(2,2) 32

(3,2) 9

(4,2) 28
(5,2) 5

(6,2) 36

(1,3) 6

(2,3) 7

(3,3) 2

(4,3) 33

(5,3) 34

(6,3) 29

(1,4) 26

(2,4) 21

(3,4) 22

(4,4) 17

(5,4) 12

(6,4) 13

(1,5) 19

(2,5) 23

(3,5) 27

(4,5) 10

(5,5) 14

(6,5) 18

(1,6) 24

(2,6) 25

(3,6) 20

(4,6) 15

(5,6) 16
(6,6) 11

>>

You might also like