>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> size(A)
ans =
3 2
>> sz = size(A)
sz =
3 2
>> size(sz)
ans =
1 2
>> size(A,1)
ans = 3
>> size(A,2)
ans = 2
>> length(A)
ans = 3
>> v = [1 2 3 4]
v =
1 2 3 4
>> length(v)
ans = 4
>> pwd
ans = C:\Users\mei\Workspaces\Octave_codes
>> ls
[.] [..] featuresX.dat priceY.dat
将dat文件加载到内存同名矩阵中:
>> load featuresX.dat
>> load priceY.dat
>> size(featuresX)
ans =
3 2
>> size(priceY)
ans =
3 1
>> load('featuresX.dat')
>> load('priceY.dat')
>> size(featuresX)
ans =
3 2
>> size(priceY)
ans =
3 1
查看当前Octave内存中所存储的所有变量:
>> who
A ans featuresX priceY sz v
>> whos
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
ans 1x2 16 double
featuresX 3x2 48 double
priceY 3x1 24 double
sz 1x2 16 double
v 1x4 32 double
Total is 23 elements using 184 bytes
>> clear sz %删除变量sz
存储数据到硬盘:
>> v = priceY(1:2) %取priceY的前两条
v =
123
258
>> save hello.mat v %存储变量到硬盘中,再次导入时还是原变量名,与文件名无关
>> ls
[.] featuresX.dat priceY.dat
[..] hello.mat
>> clear %删除内存中所有变量
>> load hello.mat %导入
>> whos
Attr Name Size Bytes Class
==== ==== ==== ===== =====
v 2x1 16 double
>> v
v =
123
258
以上是存成了二进制格式。
矩阵的复杂命令:
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> A(3,2)
ans =
6
>> A(2,:)
ans =
3 4
>> A(:,2)
ans =
2
4
6
>> A([1 3], :) %第1,3行
ans =
1 2
5 6
>> A([1 2 3], :) %第1,2,3行
ans =
1 2
3 4
5 6
>> A(:, [1]) %第1列
ans =
1
3
5
>> A(:, [2]) %第2列
ans =
2
4
6
>> A(:, [1 2]) %第1,2列
ans =
1 2
3 4
5 6
>> A
A =
1 2
3 4
5 6
>> A(:, 2) = [10; 11; 12] %修改第2列
A =
1 10
3 11
5 12
>> A = [A, [100;101;102]] %为矩阵A增加一列
A =
1 10 100
3 11 101
5 12 102
>> v = A(:) %将A中所有元素一列一列存入一个列向量中
v =
1
3
5
10
11
12
100
101
102
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> B = [11 22; 33 44; 55 66]
B =
11 22
33 44
55 66
>> C = [A B] %横着组合
C =
1 2 11 22
3 4 33 44
5 6 55 66
>> C = [A; B] %竖着组合
C =
1 2
3 4
5 6
11 22
33 44
55 66
>> [A, B] %逗号和空格一样
ans =
1 2 11 22
3 4 33 44
5 6 55 66
>> [A B]
ans =
1 2 11 22
3 4 33 44
5 6 55 66
网友评论