You are on page 1of 39

By matlab

2/2*3

ans =

By hand also the same to the math lab

By matlab

>> 8*5/4

ans =

10

It also the same with calculate by our hand

>> 8*(5/4)

ans =

10

>> 3^2/4

ans =

2.2500
It also the same result by our hand and matlab

>> 3^2^3

ans =

729

It the same that we calculated with our hand

7 - 5 * 4\9

ans =

6.5500

There is a big difference between left division and right division . but the right answer by hand is -4.25

>> 6-2/5+7^2-1

ans =

53.6000

The value is the same both with math lab and hand calculated

>> 10/2\5-3+2*4

ans =
6

The value is the same both with math lab and hand calculated

>> 2+round(6/9+3*2)/2

ans =

5.5000

But the calculated value is 5.33333333

>> 2+floor(6/9+3*2)/2

ans =

But the calculated value is 5.33333333

>> 2+ceil(6/9+3*2)/2

ans =

5.5000
But the calculated value is 5.33333333

>> x=pi/3,x=x-1,x=x+5,x=abs(x)/x

x=

1.0472

x=

0.0472

x=

5.0472

x=

In matlab the execution is in series

Matlab follow the principle of bodumas except left division

Number 2,
>> -3+2i+5-7i

ans =

2.0000 - 5.0000i

>> -3+2i-(5-7i)

ans =

-8.0000 + 9.0000i

>> -3+2i*(5-7i)

ans =

11.0000 +10.0000i

>> -3+2i/(5-7i)

ans =

-3.1892 + 0.1351i

Number3

>> x=[22:2:46]
x=

22 24 26 28 30 32 34 36 38 40 42 44 46

x=[4 5 9 6]

x=

4 5 9 6

>> x-3

ans =

1 2 6 3

x(1:2:end)+11

ans =

15 20

> sqrt(x)

ans =
2.0000 2.2361 3.0000 2.4495

>> x.^3

ans =

64 125 729 216

> x=[2:2:16]

x=

2 4 6 8 10 12 14 16

>> x=[9:-2:-5]

x=

9 7 5 3 1 -1 -3 -5

>> x = [2 1 3 7 9 4 6]

x=

2 1 3 7 9 4 6

>> x(3)

ans =
3

This means identify the number at the list of three

>> x(1:end)

ans =

2 1 3 7 9 4 6

Start from one up to last

>> x(1:end-1)

ans =

2 1 3 7 9 4

Left the last number only ( end -1)

x(2:2:6)

ans =

1 7 4

assign the number by increment two

x =[2 1 3 7 9 4 6]
x(6:-2:1)
ans =

4 7 1
Assign the number by decrement two

X=

2 1 3 7 9 4 6

>> x (end-2:-3:2)

ans =

9 1

Staring from n-2 means nine decremented by three

X=

2 1 3 7 9 4 6

sum(x)

ans =

32

X=

2 1 3 7 9 4 6

mean(x)

ans =
4.5714

X=

2 1 3 7 9 4 6

min(x)

ans =

Number 4

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

A(1,:)

ans =

X= 2 4 1

A(2:3,:)

ans =

6 7 2

3 5 9

sum(A(:,1:3))

ans =
11 16 12

sum(A,2)

ans =

15

17

NUMBER 5

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

A'

ans =

2 3 8

7 1 1

9 5 2

7 6 5

A(1, :)'

ans =

7
9

Transpose means changing the row and column . this means column 1

>> A(:, [14])

Index exceeds matrix dimensions.

But if there is space between the number

A(:, [1 4])

ans =

2 7

3 6

8 5

That means column one and four

A([23], [31])

Index exceeds matrix dimensions.

But if there is space between the number

A([2 3], [3 1])

ans =

5 3

2 8

That means column three first assign then goes to row two and three
reshape (A, 2, 6)

ans =

2 8 1 9 2 6

3 7 1 5 7 5

Reshape (A,2,6) means make the matrix two row and six column

A(:)

ans =

Change the matrix in column form


flipud (A)

ans =

8 1 2 5

3 1 5 6

2 7 9 7

Flipud flip the arrays up to down

fliplr (A)

ans =

7 9 7 2

6 5 1 3

5 2 1 8

Fliplr flip the arrays left to right

[A A(end, :)]

Error using horzcat

Dimensions of matrices being concatenated are not consistent.

[A; A(1 : 2, :)]

ans =

2 7 9 7
3 1 5 6

8 1 2 5

2 7 9 7

3 1 5 6

This means row one and two are also repeated to matrix A

sum (A)

ans =

13 9 16 18

This means adding the columns of A

> sum (A')

ans =

25 15 16

mean (A)

ans =

4.3333 3.0000 5.3333 6.0000

mean (A')

ans =
6.2500 3.7500 4.0000

>> sum (A, 2)

ans =

25

15

16

>>

mean (A, 2)

ans =

6.2500

3.7500

4.0000

min (A)

ans =

2 1 2 5

The minimum value from each column are (2 1 2 5)

max (A')
ans =

9 6 8

min (A(:, 4))

ans =

The minimum value of column four is five

[min(A)'max(A)']

Error: Unexpected MATLAB expression.

max (min(A))

ans =

[[A; sum (A)] [sum (A, 2); sum (A(:))]]

[[A; sum (A)] [sum (A, 2); sum (A(:))]]

Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.

assign the even-numbered columns of A to an array B

B=A(:,2:2:end)

B=
7 7

1 6

1 5

assign the odd-numbered rows to an array C

C=A(1:2:3,:)

C=

2 7 9 7

8 1 2 5

RECIPROCAL VALUE IS

D=A.^-1

D=

0.5000 0.1429 0.1111 0.1429

0.3333 1.0000 0.2000 0.1667

0.1250 1.0000 0.5000 0.2000

•TO convert A into a 4-by-3 array by using transpose of A

A'

ans =
2 3 8

7 1 1

9 5 2

7 6 5

compute the square-root of each element of A

sqrt(A)

ans =

1.4142 2.6458 3.0000 2.6458

1.7321 1.0000 2.2361 2.4495

2.8284 1.0000 1.4142 2.2361

to remove the second column of A

A(:,2)=[]

A=

2 9 7

3 5 6

8 2 5

add a row of all 1’s at the beginning and at the end

[1 1 1 1;A;1 1 1 1]

ans =
1 1 1 1

2 7 9 7

3 1 1 6

8 1 2 5

1 1 1 1

A(2,3)=A(3,2)

A=

2 9 7

3 5 2

8 2 5

Number 6

x = [1 3 7],

>> y = [2 4 2]

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

>> x + y

ans =

3 7 9

x+A

ans =

4 4 13

6 5 14

x' + y

ans =

3 5 3

5 7 5

9 11 9
> A - [x' y']

Matrix dimensions must agree.

The size of matrix is incompitable

[x; y] + A

ans =

4 4 13

7 6 9

[x; y]

ans =

1 3 7

2 4 2

A-3

ans =

0 -2 3

2 -1 4

A+B

Matrix dimensions must agree.

The number of column and row should be the same for addition of two vector

B' + A
ans =

4 8 8

9 10 9

If we can transpose the value of B it is compatible for addition and subtraction

B*A

ans =

23 9 34

61 23 98

16 6 26

A.*B

Matrix dimensions must agree.

BUT IF WE CAN TRANSPOSE A it is compatible to multiply the vector

2*B

ans =

2 8

14 16

4 4

2. *B
ans =

2 8

14 16

4 4

If we multiply scalar with number it is equal by using the dot in the multiplication

B./x'

ans =

1.0000 4.0000

2.3333 2.6667

0.2857 0.2857

B./[x' x']

ans =

1.0000 4.0000

2.3333 2.6667

0.2857 0.2857

2/A

Error using /

Matrix dimensions must agree.

Scalar number cannot be divided by vector because scalar vector have 1by 1 dimension
ones(1,3)* A

Error using Inner matrix dimensions must agree.

ones(1, 3) * B

ans =

10 14

Number 7

A=rand(5,5)

A=

0.4168 0.0155 0.1981 0.0527 0.9427

0.6569 0.9841 0.4897 0.7379 0.4177

0.6280 0.1672 0.3395 0.2691 0.9831

0.2920 0.1062 0.9516 0.4228 0.3015

0.4317 0.3724 0.9203 0.5479 0.7011

>> B=rand(5,1)

B=

0.6663

0.5391

0.6981
0.6665

0.1781

>> X=B/A

Error using /

Matrix dimensions must agree.

>> X=B\A

X=

0.7902 0.4680 0.8429 0.5890 1.0986

inv(A)

ans =

2.8360 2.1934 -0.0064 5.5231 -7.4861

7.9587 3.0534 -7.5007 2.3722 -3.0239

5.8430 1.6038 -5.1611 4.0644 -3.3234

-16.6654 -5.1280 13.1655 -9.1802 10.9519

-0.6205 -1.0704 0.4750 -2.8220 3.4458

>> X=inv(A)*B

X=
5.4155

2.7556

3.2721

-8.8465

-1.9261

>> A*X-B

ans =

1.0e-14 *

-0.0111

-0.0111

-0.0111

-0.0333

-0.1277

>>

Number 8

A=[3 4;6 8]
A=

3 4

6 8

>> B=[6 8;9 10]

B=

6 8

9 10

>> A.*B

ans =

18 32

54 80

>> A*B

ans =

54 64
108 128

A/B

ans =

0.5000 -0.0000

1.0000 -0.0000

A\B

ans =

-Inf -Inf

Inf Inf

NUMBER 9

A = [1:4; 5:8; 1 1 1 1]

A=

1 2 3 4

5 6 7 8

1 1 1 1

>> x = A(:, 3)
x=

B = A(1 : 3, 2 : 2)

B=

A(1, 1) = 9 + A(2, 3)

A=

16 2 3 4

5 6 7 8

1 1 1 1

A = [1:4; 5:8; 1 1 1 1]

A=

1 2 3 4
5 6 7 8

1 1 1 1

>> A(2 : 3, 1 : 3) = [0 0 0; 0 0 0]

A=

1 2 3 4

0 0 0 8

0 0 0 1

A = [1:4; 5:8; 1 1 1 1]

A=

1 2 3 4

5 6 7 8

1 1 1 1

>> A(2 : 3, 1 : 2) = [1 1; 3 3]

A=

1 2 3 4

1 1 7 8

3 3 1 1
A=[1:4; 5:8; 1 1 1 1]

A=

1 2 3 4

5 6 7 8

1 1 1 1

>> x = A(:, 3)

x=

y = A(3 : 3, 1 : 4)

y=

1 1 1 1

A = [A; 2 1 7 7; 7 7 4 5]

A=
1 2 3 4

5 6 7 8

1 1 1 1

2 1 7 7

7 7 4 5

• C = A([1, 3], 2)

C=

D = A([2, 3, 5], [1, 3, 4])

D=

5 7 8

1 1 1

7 4 5

D(2, :) = [ ]

D=

5 7 8

7 4 5

y = A(3 : 3, 1 : 4)
y=

1 1 1 1

NUMBER 10

A=rand(6,6)

A=

0.2399 0.7127 0.0714 0.1499 0.4538 0.3909

0.8865 0.5005 0.5216 0.6596 0.4324 0.8314

0.0287 0.4711 0.0967 0.5186 0.8253 0.8034

0.4899 0.0596 0.8181 0.9730 0.0835 0.0605

0.1679 0.6820 0.8175 0.6490 0.1332 0.3993

0.9787 0.0424 0.7224 0.8003 0.1734 0.5269


>> diag(diag(A))

ans =

0.2399 0 0 0 0 0

0 0.5005 0 0 0 0

0 0 0.0967 0 0 0

0 0 0 0.9730 0 0

0 0 0 0 0.1332 0

0 0 0 0 0 0.5269

>> B=A-ans+eye(6,6)

B=

1.0000 0.7127 0.0714 0.1499 0.4538 0.3909

0.8865 1.0000 0.5216 0.6596 0.4324 0.8314

0.0287 0.4711 1.0000 0.5186 0.8253 0.8034

0.4899 0.0596 0.8181 1.0000 0.0835 0.0605

0.1679 0.6820 0.8175 0.6490 1.0000 0.3993

0.9787 0.0424 0.7224 0.8003 0.1734 1.0000

>> triu(A)

ans =
0.2399 0.7127 0.0714 0.1499 0.4538 0.3909

0 0.5005 0.5216 0.6596 0.4324 0.8314

0 0 0.0967 0.5186 0.8253 0.8034

0 0 0 0.9730 0.0835 0.0605

0 0 0 0 0.1332 0.3993

0 0 0 0 0 0.5269

>> triu(A,1)

ans =

0 0.7127 0.0714 0.1499 0.4538 0.3909

0 0 0.5216 0.6596 0.4324 0.8314

0 0 0 0.5186 0.8253 0.8034

0 0 0 0 0.0835 0.0605

0 0 0 0 0 0.3993

0 0 0 0 0 0

>> triu(A,2)

ans =

0 0 0.0714 0.1499 0.4538 0.3909

0 0 0 0.6596 0.4324 0.8314


0 0 0 0 0.8253 0.8034

0 0 0 0 0 0.0605

0 0 0 0 0 0

0 0 0 0 0 0

>> z= triu(A,1)-triu(A,2)

z=

0 0.7127 0 0 0 0

0 0 0.5216 0 0 0

0 0 0 0.5186 0 0

0 0 0 0 0.0835 0

0 0 0 0 0 0.3993

0 0 0 0 0 0

>> tril(A,-1)

ans =

0 0 0 0 0 0

0.8865 0 0 0 0 0

0.0287 0.4711 0 0 0 0

0.4899 0.0596 0.8181 0 0 0

0.1679 0.6820 0.8175 0.6490 0 0


0.9787 0.0424 0.7224 0.8003 0.1734 0

>> tril(A,-2)

ans =

0 0 0 0 0 0

0 0 0 0 0 0

0.0287 0 0 0 0 0

0.4899 0.0596 0 0 0 0

0.1679 0.6820 0.8175 0 0 0

0.9787 0.0424 0.7224 0.8003 0 0

>> k = tril(A,-1)-tril(A,-2)

k=

0 0 0 0 0 0

0.8865 0 0 0 0 0

0 0.4711 0 0 0 0

0 0 0.8181 0 0 0

0 0 0 0.6490 0 0

0 0 0 0 0.1734 0

>> T = z+k+diag(diag(A))
T=

0.2399 0.7127 0 0 0 0

0.8865 0.5005 0.5216 0 0 0

0 0.4711 0.0967 0.5186 0 0

0 0 0.8181 0.9730 0.0835 0

0 0 0 0.6490 0.1332 0.3993

0 0 0 0 0.1734 0.5269

>>

You might also like