添加链接描述
向量(一维数组)
创建向量
使用linspace()创建向量
数学运算
应用函数
引用元素
连接向量
从向量中删除元素
重新排列元素
shift()
获取向量的大小
size()
length()
将向量转换为矩阵(将一维数组转换为多维数组)
norm
< Creating a vector >
Method 1 : v = [ value value value value ...]
Ex)
Input
v = [ 1 2 4 7 2 1]
Output
v =
1 2 4 7 2 1
Method 2 : v = start:step:end
Ex)
Input
v=1:0.2:2
Output
v =
1.0000 1.2000 1.4000 1.6000 1.8000 2.0000
< Creating a vector with linspace >
Method 1 : v = linspace(start_value,end_value,num_of_data)
Ex)
Input
v = linspace(1,4,10)
Output
v =
1.0000 1.3333 1.6667 2.0000 2.3333 2.6667 3.0000 3.3333 3.6667 4.0000
< Mathematical Operation >
Case 1 : v = vector1 + vector2
Ex)
Input
v1=[1 2 3 4];
v2=[5 6 7 8];
v=v1 + v2
Output
v =
6 8 10 12
Case 2 : v = vector1 - vector2
Ex)
Input
v1=[1 2 3 4];
v2=[5 6 7 8];
v=v1 - v2
Output
v =
-4 -4 -4 -4
Case 3 : v = vector1 . vector2 (Inner Product)
Ex)
Input
v1=[1 2 3 4];
v2=[5 6 7 8];
v=v1 * v2' // Note : I put the transpose operator (') here. Try v = v1*v2 and see what happen.
Output
v =
70
Case 4 : v = vector1 x vector2 (multiplication of each elements)
Ex)
Input
v1=[1 2 3 4];
v2=[5 6 7 8];
v=v1 .* v2 // Note : I used .*, not * . Try v = v1*v2 and see what happen.
Output
v =
5 12 21 32
Case 5 : v = vector1 / vector2 (division of each elements)
Ex)
Input
v1=[1 2 3 4];
v2=[5 6 7 8];
v=v1 ./ v2 // Note : I used ./, not /
Output
v =
0.20000 0.33333 0.42857 0.50000
Case 6 : v = scalar + vector2
Ex)
Input
v1 =[1 2 3 4];
s = 2;
v= s + v1 // Note : In this case, v = s .+ v1 will give the same result
Output
v =
3 4 5 6
Case 7 : v = scalar x vector2
Ex)
Input
v1 =[1 2 3 4];
s = 2;
v= s .* v1 // Note : In this case, v = s * v1 will give the same result
Output
v =
2 4 6 8
< Applying functions >
Case 1 : v = function(vector)
Ex)
Input
v1 =[1 2 3 4];
v= sin(v1)
Output
v =
0.84147 0.90930 0.14112 -0.75680
Ex)
Input
v1 =[1 2 3 4];
v= v1.^2 + 2 .* v1 + cos(v1)
Output
v =
3.5403 7.5839 14.0100 23.3464
< Referencing the elements >
Case 1 : v = vector (index)
Ex)
Input
v1 =[1 2 3 4];
v= v1(3)
Output
v =
3
Case 2 : v = vector ([index range])
Ex)
Input
v1 =[1 2 3 4];
v= v1([1:3])
Output
v =
1 2 3
Case 3 : v = vector ([index list])
Ex)
Input
v1 =[1 2 3 4];
v= v1([4 2 3])
Output
v =
4 2 3
Case 4 : v = vector (end)
Ex)
Input
v1 =[1 2 3 4];
v= v1(end)
Output
v =
4
< Concatenating Vectors >
Case 1 : v = [v1 v2] // All the logic is same
Ex)
Input
v1 =[1 2 3 4];
v2 =[5 6 7 8];
v= [v1 v2]
Output
v =
1 2 3 4 5 6 7 8
Ex)
Input
v1 =[1 2 3 4];
v2 =[5 6 7 8];
v= [v1 v2 -v1]
Output
v =
1 2 3 4 5 6 7 8 -1 -2 -3 -4
Ex)
Input
v=[]; // create a variable named 'v' and initialize it with an empty array
for i = 1:10
v=[v i];
end
v
Output
v =
1 2 3 4 5 6 7 8 9 10
< Removing/Deleing Elements >
Case 1 : Removing/Deleting one element from a Vector
Ex)
Input
v = [1 2 3 4 5];
v(3) = []
Output
v =
1 2 4 5
Case 2 : Removing/Deleting multiple elements from a Vector
Ex)
Input
v = [1 2 3 4 5];
v([2 5]) = []
Output
v =
1 3 4
< Rearranging Elements - shift() >
Case 1 : v = shift(vector,N) // Where N is a Positive Number
Ex)
Input
v1 = [1 2 3 4 5 6 7 8 9 10];
v = shift(v1,3)
Output
v =
8 9 10 1 2 3 4 5 6 7
Case 2 : v = shift(vector,N) // Where N is a Negative Number
Ex)
Input
v1 = [1 2 3 4 5 6 7 8 9 10];
v = shift(v1,-3)
Output
v =
4 5 6 7 8 9 10 1 2 3
< Getting the size of a vector : size() >
Case 1 : v = size(v) // returns number of cols and number of rows of v
Ex)
Input
t = 0:0.1:10;
size(t)
Output
ans =
1 101
Case 2 : v = size(v,1) // returns the number of rows of v
Ex)
Input
t = 0:0.1:10;
size(t,1)
Output
ans =
1
Case 3 : v = size(v,2) // returns the number of rows of v
Ex)
Input
t = 0:0.1:10;
size(t,2)
Output
ans =
101
< Getting the size of a vector : length() >
Case 1 : v = length(v) // returns number of elements of v
Ex)
Input
t = 0:0.1:10;
length(t)
Output
ans =
101
< Converting a Vector into a Matrix (Converting one dimmensional array into multi dimensional array) >
Case 1 : v = reshape(vector,rows,cols)
Ex)
Input
v1 = [1 2 3 4 5 6 7 8 9 10];
v = reshape(v1,2,5) // Note : Number of matrix elements and Vector elements should be same
Output
v =
1 2 3 4 5
6 7 8 9 10
< norm >
Case 1 : v_n = norm(realvector);
Ex)
Input
r = [1 2 3 2 5];
r_n = norm(r);
Output
r_n =
6.5574
norm() performs following procedure.
Input
r = [1 2 3 2 5];
r_n = sqrt(sum(abs(r).^2));
Output
r_n =
6.5574
Case 2 : v_n = norm(complexvector);
Ex)
Input
c = [1+2*j 2+5*j 3+2*j 4-2*j];
c_n = norm(c);
Output
c_n =
8.1854
norm() performs following procedure.
Input
c = [1 2 3 2 5];
c_n = sqrt(sum(abs(c).^2));
Output
c_n =
8.1854```