v1=5:3:15
v1 =
5 8 11 14
v2=linspace(5,15,20)
v2 =
Columns 1 through 6
5.0000 5.5263 6.0526 6.5789 7.1053 7.6316
Columns 7 through 12
8.1579 8.6842 9.2105 9.7368 10.2632 10.7895
Columns 13 through 18
11.3158 11.8421 12.3684 12.8947 13.4211 13.9474
Columns 19 through 20
14.4737 15.0000
v=1:5;
help round
round Round towards nearest integer.
round(X) rounds the elements of X to the nearest integers.
See also floor, ceil, fix.
Overloaded methods:
codistributed/round
gpuArray/round
Reference page in Help browser
doc round
v=[1.8 -1.8 2.5 -2.5 1.1 -1.1]
v =
1.8000 -1.8000 2.5000 -2.5000 1.1000 -1.1000
round(v)
ans =
2 -2 3 -3 1 -1
floor(v)
ans =
1 -2 2 -3 1 -2
ceil(v)
ans =
2 -1 3 -2 2 -1
v
v =
1.8000 -1.8000 2.5000 -2.5000 1.1000 -1.1000
fix(v)
ans =
1 -1 2 -2 1 -1
v=1:5;
5+8
ans =
13
5*v
ans =
5 10 15 20 25
v*5
ans =
5 10 15 20 25
v+5
ans =
6 7 8 9 10
v-5
ans =
-4 -3 -2 -1 0
v/5
ans =
0.2000 0.4000 0.6000 0.8000 1.0000
v\5
ans =
0
0
0
0
1
v^2
{Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
}
v.^2
ans =
1 4 9 16 25
v*v
{Error using *
Inner matrix dimensions must agree.
}
v*v'
ans =
55
v'*v
ans =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
v+v1
{Error using +
Matrix dimensions must agree.
}
v+v
ans =
2 4 6 8 10
v-v
ans =
0 0 0 0 0
v/v
ans =
1.0000
v./v
ans =
1 1 1 1 1
a=1:4;
M=[a;a+4;a+8]
M =
1 2 3 4
5 6 7 8
9 10 11 12
b=[1 2 3 5:9 -1]
b =
1 2 3 5 6 7 8 9 -1
M
M =
1 2 3 4
5 6 7 8
9 10 11 12
M+4
ans =
5 6 7 8
9 10 11 12
13 14 15 16
M-4
ans =
-3 -2 -1 0
1 2 3 4
5 6 7 8
M*4
ans =
4 8 12 16
20 24 28 32
36 40 44 48
M/4
ans =
0.2500 0.5000 0.7500 1.0000
1.2500 1.5000 1.7500 2.0000
2.2500 2.5000 2.7500 3.0000
M.+M
M.+M
|
{Error: Unexpected MATLAB operator.
}
M.*M
ans =
1 4 9 16
25 36 49 64
81 100 121 144
M*a'
ans =
30
70
110
M*a
{Error using *
Inner matrix dimensions must agree.
}
M/a
ans =
1.0000
2.3333
3.6667
9/3
ans =
3
M\a
{Error using \
Matrix dimensions must agree.
}
M*M'
ans =
30 70 110
70 174 278
110 278 446
M'*M
ans =
107 122 137 152
122 140 158 176
137 158 179 200
152 176 200 224
M1=[M (1:3)']
M1 =
1 2 3 4 1
5 6 7 8 2
9 10 11 12 3
M1=[M 1;2;3]
{Error using horzcat
Dimensions of matrices being concatenated are not consistent.
}
M1=[M (1:3)']
M1 =
1 2 3 4 1
5 6 7 8 2
9 10 11 12 3
M1=[M [1;2;3]]
M1 =
1 2 3 4 1
5 6 7 8 2
9 10 11 12 3
M+M
ans =
2 4 6 8
10 12 14 16
18 20 22 24
M+M1
{Error using +
Matrix dimensions must agree.
}
M-M
ans =
0 0 0 0
0 0 0 0
0 0 0 0
M'*M1
ans =
107 122 137 152 38
122 140 158 176 44
137 158 179 200 50
152 176 200 224 56
M'.*M1
{Error using .*
Matrix dimensions must agree.
}
M/M
[Warning: Rank deficient, rank = 2, tol = 9.378592e-15.]
ans =
1.0000 0 0
0.5000 0 0.5000
0 0 1.0000
a(1)
ans =
1
a=5:10;
a(1)
ans =
5
a
a =
5 6 7 8 9 10
a(4/2)
ans =
6
a(4/3)
{Subscript indices must either be real positive integers or
logicals.
}
a(1:2)
ans =
5 6
a([2 3])
ans =
6 7
a=10:18;
a([2 3])
ans =
11 12
a=11:18;
a([2 3])
ans =
12 13
a([3 2])
ans =
13 12
a([3 2 2 2])
ans =
13 12 12 12
a([3 2 2 2 12])
{Index exceeds matrix dimensions.
}
a(2:4)
ans =
12 13 14
a([2:4])
ans =
12 13 14
a([2:4 1])
ans =
12 13 14 11
a([2:2:8])
ans =
12 14 16 18
a([4 1 9/3])
ans =
14 11 13
a([2:4 1:3 7])
ans =
12 13 14 11 12 13 17
a(:)
ans =
11
12
13
14
15
16
17
18
a(end)
ans =
18
a(end-1)
ans =
17
a(3:end)
ans =
13 14 15 16 17 18
a(end:-1:1)
ans =
18 17 16 15 14 13 12 11
M
M =
1 2 3 4
5 6 7 8
9 10 11 12
M(3)
ans =
9
M(5)
ans =
6
M(10)
ans =
4
M
M =
1 2 3 4
5 6 7 8
9 10 11 12
M(:)
ans =
1
5
9
2
6
10
3
7
11
4
8
12
M
M =
1 2 3 4
5 6 7 8
9 10 11 12
M(2,2)
ans =
6
M(2,2:4)
ans =
6 7 8
M([1:2 1],2:3)
ans =
2 3
6 7
2 3
M
M =
1 2 3 4
5 6 7 8
9 10 11 12
M(1,:)
ans =
1 2 3 4
M([1 end],:)
ans =
1 2 3 4
9 10 11 12
M(:,2:3)
ans =
2 3
6 7
10 11
M(1:2,3:4)
ans =
3 4
7 8
M
M =
1 2 3 4
5 6 7 8
9 10 11 12
M(:,:)
ans =
1 2 3 4
5 6 7 8
9 10 11 12
v
v =
1 2 3 4 5
v(2)=10
v =
1 10 3 4 5
v(2)=[10 20]
{In an assignment A(I) = B, the number of elements in B and I must
be the same.
}
v(2:3)=[10 20]
v =
1 10 20 4 5
v(:)=-5
v =
-5 -5 -5 -5 -5
v=1:6;
v(2:3)=0
v =
1 0 0 4 5 6
v(1:2)=v(3:4)
v =
0 4 0 4 5 6
v=1:6;
v(1:2)=v([5 4])
v =
5 4 3 4 5 6
M
M =
1 2 3 4
5 6 7 8
9 10 11 12
M(3)=100
M =
1 2 3 4
5 6 7 8
100 10 11 12
M(3,:)=-5
M =
1 2 3 4
5 6 7 8
-5 -5 -5 -5
M(3,:)=[-5 5]
{Subscripted assignment dimension mismatch.
}
M(3,:)=[-5 5 8 9]
M =
1 2 3 4
5 6 7 8
-5 5 8 9
M(3,:)=a(1:4)
M =
1 2 3 4
5 6 7 8
11 12 13 14
M(:,1)=a(1:3)
M =
11 2 3 4
12 6 7 8
13 12 13 14
M1=M;
M(:)=-4
M =
-4 -4 -4 -4
-4 -4 -4 -4
-4 -4 -4 -4
M=M1;
M(1,:)=M(3,:)
M =
13 12 13 14
12 6 7 8
13 12 13 14
M(1,:)=M(3,:)*10
M =
130 120 130 140
12 6 7 8
13 12 13 14
M(1:2,3:4)
ans =
130 140
7 8
M
M =
130 120 130 140
12 6 7 8
13 12 13 14
M(1:2,3:4)=[10 20;30 40]
M =
130 120 10 20
12 6 30 40
13 12 13 14
diary off