ocvate常用函数
1.生成矩阵相关
https://www.coursera.org/learn/machine-learning/lecture/9fHfl/basic-operations
1. 初始化矩阵
a = [1,2; 3,4; 5,6]
a =
1 2
3 4
5 6
v = 1: 6
v =
1 2 3 4 5 6
v = 1: 1: 10
v =
1 2 3 4 5 6 7 8 9 10
ones全1矩阵、
zeros全0矩阵、
rand随机矩阵(0 - 1)、
eye单位矩阵
one = ones(3, 3)
one =
1 1 1
1 1 1
1 1 1
zero = zeros(1, 3) zero = 0 0 0
rand = rand(1, 3)
rand =
0.55265 0.42458 0.14323
eye = eye(3)eye = Diagonal Matrix 1 0 0 0 1 0 0 0 1
2.矩阵操作
A = 1 2 3 4 5 6
1.取某个元素
A(2,2)
ans = 4
2.取某一行
A(2,:)
ans =
3 4
3.取某几行
A([1,3],:)
ans =
1 2
5 6
4.取某列
A(:,2)
ans =
2
4
6
5.取某几列
6.修改某行或某列
A(:,2) = [4;6;8]
A =
1 4
3 6
5 8
7.追加某行或某列
A = [A,[6;8;10]]
A =
1 4 6
3 6 8
5 8 10
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
B = [1;2;3] B = 1 2 3 >> C = [A B] C = 1 4 6 1 3 6 8 2 5 8 10 3
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
>> B = [1;2;3] B = 1 2 3 >> D= [4;5;6] D = 4 5 6 >> [B;D] ans = 1 2 3 4 5 6
8.转换为列向量
A(:)
ans =
1
3
5
4
6
8
6
8
10
9.取size
A = [1,2;3,4;5,6] A = 1 2 3 4 5 6 >> size(A) ans = 3 2 ans_1 = size(A, 1) ans_1 = 3 >> ans_2 = size(A, 2) ans_2 = 2
10.取长度,其实是最长维度的长度
V = [1,2,3,4] V = 1 2 3 4 >> length(V) ans = 4
>> A = [1,2;3,4;5,6] A = 1 2 3 4 5 6 >> length(A) ans = 3
3.命令行操作
1.输出当前所在目录
pwd
2.打印包含的文件
ls
3.切换目录
cd '../xxx'
4.载入数据(当前目录)
data1 = load('xxx.txt')
5.保存数据(当前目录)
save ans.txt v
save 文件名 数据