0% found this document useful (0 votes)
5 views

Mathlab Day 1

Uploaded by

u1902148
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Mathlab Day 1

Uploaded by

u1902148
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

MATLAB Command Window Page 1

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

A =

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

>> B=1:5

B =

1 2 3 4 5

>> C=(1:5)'

C =

1
2
3
4
5

>> % extracting sub matrix


>> A(2,3)

ans =

>> A(2:,3:)
A(2:,3:)

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

>> A(2:end,3:end)

ans =

8 9 10
13 14 15

>> A(:,2:4)

ans =

2 3 4
7 8 9
MATLAB Command Window Page 2

12 13 14

>> %Repleceing elements


>> A(3,4)==30

ans =

logical

>> A(3,4)=30

A =

1 2 3 4 5
6 7 8 9 10
11 12 13 30 15

>> A(3,:)=30

A =

1 2 3 4 5
6 7 8 9 10
30 30 30 30 30

>> A(3,:)=[1 3 4 5 6]

A =

1 2 3 4 5
6 7 8 9 10
1 3 4 5 6

>> % Deleting elements


>> B=[1 2 3 4 5 6 7]

B =

1 2 3 4 5 6 7

>> B(1,5)=[]
A null assignment can have only one non-colon index.

>> B(1,7)=[]
A null assignment can have only one non-colon index.

>> B(:,7)=[]
MATLAB Command Window Page 3

B =

1 2 3 4 5 6

>> B(:,2:4)=[]

B =

1 5 6

>> A

A =

1 2 3 4 5
6 7 8 9 10
1 3 4 5 6

>> A(:,2:4)=[]

A =

1 5
6 10
1 6

>> A(2,:)=[]

A =

1 5
1 6

>> % Creating 3D matrix


>> p=[1 2 3; 4 5 6; 1 2 1]

p =

1 2 3
4 5 6
1 2 1

>> q=[1 1 1; 2 2 2; 3 3 3]

q =

1 1 1
2 2 2
MATLAB Command Window Page 4

3 3 3

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

r =

3 2 1
6 5 4
9 8 7

>> A=cat(3,p,q,r)

A(:,:,1) =

1 2 3
4 5 6
1 2 1

A(:,:,2) =

1 1 1
2 2 2
3 3 3

A(:,:,3) =

3 2 1
6 5 4
9 8 7

>> %Concatenate
>> A(:,:,1)

ans =

1 2 3
4 5 6
1 2 1

>> % alternative
>> p

p =

1 2 3
4 5 6
1 2 1
MATLAB Command Window Page 5

>> q

q =

1 1 1
2 2 2
3 3 3

>> r

r =

3 2 1
6 5 4
9 8 7

>> A=p

A =

1 2 3
4 5 6
1 2 1

>> A(:,:,2)=q

A(:,:,1) =

1 2 3
4 5 6
1 2 1

A(:,:,2) =

1 1 1
2 2 2
3 3 3

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

A(:,:,1) =

1 2 3
4 5 6
1 2 1
MATLAB Command Window Page 6

A(:,:,2) =

1 1 1
2 2 2
3 3 3

A(:,:,3) =

3 2 1
6 5 4
9 8 7

>> size(A)

ans =

3 3 3

>> % Random Matrix


>> A=rand(3,2)

A =

0.8147 0.9134
0.9058 0.6324
0.1270 0.0975

>> A=rand(3,5)

A =

0.2785 0.9649 0.9572 0.1419 0.7922


0.5469 0.1576 0.4854 0.4218 0.9595
0.9575 0.9706 0.8003 0.9157 0.6557

>> 9*rand(3,5)

ans =

0.3214 6.1086 3.5300 6.3544 0.4155


7.6422 6.8197 5.8993 0.2865 0.8742
8.4059 6.6882 1.5407 2.4923 7.4111

>> 1+9*rand(3,5)

ans =

7.2535 1.3100 7.8897 5.4079 7.3843


MATLAB Command Window Page 7

3.8539 4.9487 8.1568 5.0103 7.7922


9.5520 4.4340 2.6819 6.8168 3.4842

>> 9*rand(3,3)% it makes the range 0 to 9

ans =

6.1173 1.0710 3.0635


5.8959 4.4853 5.2674
1.4635 8.6377 2.0143

>> 9*rand(4,5)% it makes the range 0 to 9

ans =

6.7614 8.0181 1.3436 7.3286 1.7694


2.2959 8.6336 2.3176 2.1917 2.2598
4.5536 4.9249 7.5665 8.3634 5.5444
6.2917 1.2476 2.2885 3.1499 4.2596

>> 9*rand(3,5)% it makes the range 0 to 9

ans =

3.1649 4.9475 6.8148 5.1104 4.7772


7.4775 8.2547 6.7836 0.6827 7.0125
5.2674 2.5726 3.4240 0.4856 8.4061

>> 1+9*rand(3,5)% it makes the range 1 to 10

ans =

2.1692 1.1071 8.1486 2.4908 6.8867


6.1194 4.0341 3.8009 6.4178 7.2029
5.2245 2.4596 5.7568 3.3667 7.7334

>> (5-2)*rand(3,5)% it will subtract from range 0 to 5 to range 0 to 2

ans =

1.3516 2.7400 1.6150 1.3280 0.0139


0.2515 0.4571 2.9884 0.3200 2.3247
0.6869 2.4775 0.2345 2.8857 2.4519

>> 2+(5-2)*rand(3,5)% it makes the range from 2 to 5

ans =

4.6061 2.7796 4.7319 2.4366 3.7391


MATLAB Command Window Page 8

2.2533 4.4002 2.5455 2.4082 3.6496


3.1993 3.2942 2.7914 4.6079 2.4349

>> 4+(10-4)*rand(3,5)% it makes the range from 4 to 10

ans =

9.1182 7.0795 5.4395 5.4397 9.4163


7.7323 6.4108 4.7399 6.5036 9.6687
6.1057 4.4558 5.1034 4.2979 6.9452

>> help randi


randi Pseudorandom integers from a uniform discrete distribution.
R = randi(IMAX,N) returns an N-by-N matrix containing pseudorandom
integer values drawn from the discrete uniform distribution on 1:IMAX.
randi(IMAX,M,N) or randi(IMAX,[M,N]) returns an M-by-N matrix.
randi(IMAX,M,N,P,...) or randi(IMAX,[M,N,P,...]) returns an
M-by-N-by-P-by-... array. randi(IMAX) returns a scalar.
randi(IMAX,SIZE(A)) returns an array the same size as A.

R = randi([IMIN,IMAX],...) returns an array containing integer


values drawn from the discrete uniform distribution on IMIN:IMAX.

Note: The size inputs M, N, P, ... should be nonnegative integers.


Negative integers are treated as 0.

R = randi(..., CLASSNAME) returns an array of integer values of class


CLASSNAME.

R = randi(..., 'like', Y) returns an array of integer values of the


same class as Y.

The arrays returned by randi may contain repeated integer values. This
is sometimes referred to as sampling with replacement. To get unique
integer values, sometimes referred to as sampling without replacement,
use RANDPERM.

The sequence of numbers produced by randi is determined by the settings of


the uniform random number generator that underlies RAND, RANDN, and randi.
randi uses one uniform random value to create each integer random value.
Control that shared random number generator using RNG.

Examples:

Example 1: Generate integer values from the uniform distribution on


the set 1:10.
r = randi(10,100,1);

Example 2: Generate an integer array of integer values drawn uniformly


MATLAB Command Window Page 9

from 1:10.
r = randi(10,100,1,'uint32');

Example 3: Generate integer values drawn uniformly from -10:10.


r = randi([-10 10],100,1);

Example 4: Reset the random number generator used by RAND, randi, and
RANDN to its default startup settings, so that randi produces the same
random numbers as if you restarted MATLAB.
rng('default');
randi(10,1,5)

Example 5: Save the settings for the random number generator used by
RAND, randi, and RANDN, generate 5 values from randi, restore the
settings, and repeat those values.
s = rng
i1 = randi(10,1,5)
rng(s);
i2 = randi(10,1,5) % i2 contains exactly the same values as i1

Example 6: Reinitialize the random number generator used by RAND,


randi, and RANDN with a seed based on the current time. randi will
return different values each time you do this. NOTE: It is usually
not necessary to do this more than once per MATLAB session.
rng('shuffle');
randi(10,1,5)

See also rand, randn, randperm, rng, RandStream

Reference page for randi


Other functions named randi

>> randi(10,3)

ans =

5 4 4
4 2 3
10 8 5

>> randi(10,5)

ans =

1 1 1 5 7
2 3 2 6 2
10 4 7 3 4
10 9 8 8 7
6 1 7 2 8
MATLAB Command Window Page 10

>> randi(10,5,2)

ans =

1 5
10 4
8 6
5 6
5 9

>> randi(10,2,5)

ans =

8 4 6 10 6
7 9 4 9 7

>> % zeros and ones


>> zeros(3)

ans =

0 0 0
0 0 0
0 0 0

>> zeros(3,4)

ans =

0 0 0 0
0 0 0 0
0 0 0 0

>> ones(4)

ans =

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

>> ones(2,3)

ans =

1 1 1
MATLAB Command Window Page 11

1 1 1

>> %Mathematical operations


>> 1+5

ans =

>> plus(1,5)

ans =

>> isequal(1+5,plus(1,5))

ans =

logical

>> 1+5==plus(1,5)

ans =

logical

>> 5*6

ans =

30

>> times(5,6)

ans =

30

>> 2^3

ans =

8
MATLAB Command Window Page 12

>> power(2,3)

ans =

>> sqrt(4)

ans =

>> sqrt(16)

ans =

>> A=1:5

A =

1 2 3 4 5

>> B=1:2:10

B =

1 3 5 7 9

>> A==B

ans =

1×5 logical array

1 0 0 0 0

>> sum(A)

ans =

15

>> prod(A)

ans =

120
MATLAB Command Window Page 13

>> A=[1:5;2:6;3:7]

A =

1 2 3 4 5
2 3 4 5 6
3 4 5 6 7

>> sum(A)

ans =

6 9 12 15 18

>> B=A(:)% combining columns

B =

1
2
3
2
3
4
3
4
5
4
5
6
5
6
7

>> sum(A(:)) % total sum

ans =

60

>> sum(sum(A)) % alter

ans =

60

>> A
MATLAB Command Window Page 14

A =

1 2 3 4 5
2 3 4 5 6
3 4 5 6 7

>> A'

ans =

1 2 3
2 3 4
3 4 5
4 5 6
5 6 7

>> avg(A)
Undefined function or variable 'avg'.

>> B=A'

B =

1 2 3
2 3 4
3 4 5
4 5 6
5 6 7

>> A+B
Matrix dimensions must agree.

>> a=[1 2; 2 3; 4 5];


>> b=[2 1; 3 2; 5 4];
>> a+b

ans =

3 3
5 5
9 9

>> a

a =

1 2
2 3
4 5
MATLAB Command Window Page 15

>> b

b =

2 1
3 2
5 4

>> a*b% multiplication


Error using *
Inner matrix dimensions must agree.

>> % m by n * n by p exist only


>> a

a =

1 2
2 3
4 5

>> b=a'

b =

1 2 4
2 3 5

>> a*b

ans =

5 8 14
8 13 23
14 23 41

>> % element wise multiplication


>> a

a =

1 2
2 3
4 5

>> b

b =
MATLAB Command Window Page 16

1 2 4
2 3 5

>> c=[2 3; 1 2; 4 3]

c =

2 3
1 2
4 3

>> a.*c

ans =

2 6
2 6
16 15

>> a.*b
Matrix dimensions must agree.

>> a^2% a*a


Error using ^
One argument must be a square matrix and the other must be a scalar. Use POWER
(.^) for elementwise power.

>> a=randi(10,3)

a =

6 5 2
3 3 3
4 9 2

>> a^2 % a*a

ans =

59 63 31
39 51 21
59 65 39

>> a.^2

ans =

36 25 4
MATLAB Command Window Page 17

9 9 9
16 81 4

>> det(a)

ans =

-66

>> abs(det(a))

ans =

66

>> a^-1

ans =

0.3182 -0.1212 -0.1364


-0.0909 -0.0606 0.1818
-0.2273 0.5152 -0.0455

>> 1/a
Error using /
Matrix dimensions must agree.

>> inverse(a)
Undefined function or variable 'inverse'.

>> inv(a)

ans =

0.3182 -0.1212 -0.1364


-0.0909 -0.0606 0.1818
-0.2273 0.5152 -0.0455

>> factorial(5)

ans =

120

>> remainder(10,2)
Undefined function or variable 'remainder'.

>> rem(10,20
rem(10,20
MATLAB Command Window Page 18

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

Did you mean:


>> rem(10,20)

ans =

10

>> rem(10,2)

ans =

>> rem(10,3)

ans =

>> max(a)

ans =

6 9 3

>> max(max(a))

ans =

>> min(min(a))

ans =

>> % Matrix indexing


>> A=[1:20;20:40]
Dimensions of matrices being concatenated are not consistent.

>> A=[1:20;21:40]

A =

Columns 1 through 13
MATLAB Command Window Page 19

1 2 3 4 5 6 7 8 9 10 11 12 13
21 22 23 24 25 26 27 28 29 30 31 32 33

Columns 14 through 20

14 15 16 17 18 19 20
34 35 36 37 38 39 40

>> A=[1:20;21:40]

A =

1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39 40

>> A>>30
A>>30

Error: Unexpected MATLAB operator.

>> A>30

ans =

2×20 logical array

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1

>> A<20 & A>30

ans =

2×20 logical array

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

>> A<20 | A>30

ans =

2×20 logical array

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
MATLAB Command Window Page 20

>> A(A>20)=1

A =

1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1

>> A=1:5:50

A =

1 6 11 16 21 26 31 36 41 46

>> B=1:10

B =

1 2 3 4 5 6 7 8 9 10

>> A(B(1,8))

ans =

36

>> B=3:3:30

B =

3 6 9 12 15 18 21 24 27 30

>> length(A)==length(B)

ans =

logical

>> B=3:3:50

B =

3 6 9 12 15 18 21 24 27 30 33 36 39 42
45 48
MATLAB Command Window Page 21

>> A(B(1,5))
Index exceeds matrix dimensions.

>> B(A(1,5))
Index exceeds matrix dimensions.

>>

You might also like