矩阵的维度
>>A=[1 2;3 4;5 6]
A =
1 2
3 4
5 6
>>sz = size(A) %三行两列
sz =
3 2
>>size(sz) %一行两列
ans =
1 2
>>size(A,1) %返回A的行数
ans = 3
>>size(A,2) %返回A的列数
ans = 2
>>v = [1 2 3 4]
v =
1 2 3 4
>>length(v) %length()返回最大纬度
ans = 4
>>length([1;2;3;4;5])
ans = 5
读取和储存数据
featuresX.dat和priceY.dat文件官网下载传送门
>>cd c:\Users\eric.zhai\Desktop
>>pwd
ans = c:\Users\eric.zhai\Desktop
>>load('featuresX.dat') %加载文件
>>who %who能让你看到当前工作空间中的所有变量
Variables in the current scope:
A I a b featuresX v
C V ans c sz w
>>featuresX
featuresX =
2104 3
1600 3
2400 3
1416 2
3000 4
1985 4
1534 3
1427 3
1380 3
1494 3
1940 4
2000 3
1890 3
4478 5
1268 3
1437 3
1239 3
2132 4
4215 4
2162 4
1664 2
2238 3
2567 4
1200 3
852 2
1852 4
1203 3
>>size(featuresX)
ans =
27 2
>>load('priceY.dat')
>>size(priceY)
ans =
27 1
>>who
Variables in the current scope:
A I a b featuresX sz w
C V ans c priceY v
>>whos %看的更清楚
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 3x2 48 double
C 2x3 48 double
I 6x6 48 double
V 1x11 24 double
a 1x1 8 double
ans 1x2 16 double
b 1x2 2 char
c 1x1 1 logical
featuresX 27x2 432 double
priceY 27x1 216 double
sz 1x2 16 double
v 1x4 32 double
w 1x10000 80000 double
Total is 10152 elements using 80891 bytes
>>clear featuresX %删除变量
>>who
Variables in the current scope:
A C I V a ans b c priceY sz v w
>>V = priceY(1:10) %将priceY前10元素保存在变量V中
V =
3999
3299
3690
2320
5399
2999
3149
1989
2120
2425
>>save hello.mat v %将变量V以二进制形式保存在hello.mat的文件,默认在桌面
>>save hello.txt v -ascii %将数据的ascii码转成文本形式
操作数据
>>A = [1 2;3 4;5 6] %A是两行三列矩阵
A =
1 2
3 4
5 6
>>A(3,2) %第3行第2列的元素
ans = 6
>>A(2,:) %第2行的全部元素
ans =
3 4
>>A(:,2) %第2列的全部元素
ans =
2
4
6
>>A(:,2) = [10;11;12] %将A的第2列换成10;11;12
A =
1 10
3 11
5 12
>>A = [A,[100;101;102]] %追加1列
A =
1 10 100
3 11 101
5 12 102
>>A(:) %全部按列转成列向量
ans =
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 12;13 14;15 16]
B =
11 12
13 14
15 16
>>C = [A B] %按列拼接
C =
1 2 11 12
3 4 13 14
5 6 15 16
>>C = [A;B] %按行拼接
C =
1 2
3 4
5 6
11 12
13 14
15 16
>>C = [B A] %C = [B A]和C = [B ,A]是一样的
C =
11 12 1 2
13 14 3 4
15 16 5 6
>>C = [B ,A]
C =
11 12 1 2
13 14 3 4
15 16 5 6