Mathlab Day 1
Mathlab Day 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
ans =
>> A(2:,3:)
A(2:,3:)
>> 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
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
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
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
>> 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
A =
0.8147 0.9134
0.9058 0.6324
0.1270 0.0975
>> A=rand(3,5)
A =
>> 9*rand(3,5)
ans =
>> 1+9*rand(3,5)
ans =
ans =
ans =
ans =
ans =
ans =
ans =
ans =
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.
Examples:
from 1:10.
r = randi(10,100,1,'uint32');
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
>> 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
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
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 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 =
1
2
3
2
3
4
3
4
5
4
5
6
5
6
7
ans =
60
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.
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 =
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
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=randi(10,3)
a =
6 5 2
3 3 3
4 9 2
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 =
>> 1/a
Error using /
Matrix dimensions must agree.
>> inverse(a)
Undefined function or variable 'inverse'.
>> inv(a)
ans =
>> factorial(5)
ans =
120
>> remainder(10,2)
Undefined function or variable 'remainder'.
>> rem(10,20
rem(10,20
MATLAB Command Window Page 18
ans =
10
>> rem(10,2)
ans =
>> rem(10,3)
ans =
>> max(a)
ans =
6 9 3
>> max(max(a))
ans =
>> min(min(a))
ans =
>> 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
>> A>30
ans =
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
ans =
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
ans =
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.
>>