一、randperm命令
1.随机产生N个1到N的整数
P = randperm(N) returns a vector containing a random permutation of the integers 1:N. For example, randperm(6) might be [2 4 5 6 1 3].
返回一个N维向量P,包含N个1到N的随机值,且不重复!
>> P=randperm(6)
P =
4 5 2 1 6 3
>> P=randperm(6)
P =
4 6 2 1 3 5
2.随机产生K个1到N的整数
P = randperm(N,K) returns a row vector containing K unique integers selected randomly from 1:N. For example, randperm(6,3) might be [4 2 5].
返回一个K维向量P,包含K个1到N的随机值,且不重复!
>> P=randperm(6,5)
P =
1 5 6 2 3
>> P=randperm(6,5)
P =
5 4 3 6 2
二、reshape命令
1.重塑矩阵
reshape(X,M,N) or reshape(X,[M,N]) returns the M-by-N matrix whose elements are taken columnwise from X. An error results if X does not have M*N elements.
将矩阵Xm×n重塑成一个M×N的矩阵,按每一列从上往下取,其中m∗n=M∗N,否则将产生错误。
>> X=rand(3,4)
X =
0.5519 0.4872 0.2137 0.9010
0.4038 0.3848 0.5439 0.0563
0.7501 0.0614 0.4106 0.4435
>> reshape(X,4,3)
ans = %注意观察是从X的每一列开始取
0.5519 0.3848 0.4106
0.4038 0.0614 0.9010
0.7501 0.2137 0.0563
0.4872 0.5439 0.4435
>> reshape(X,6,2)
ans =
0.5519 0.2137
0.4038 0.5439
0.7501 0.4106
0.4872 0.9010
0.3848 0.0563
0.0614 0.4435
>> reshape(X,2,6)
ans =
0.5519 0.7501 0.3848 0.2137 0.4106 0.0563
0.4038 0.4872 0.0614 0.5439 0.9010 0.4435
当然,矩阵X也可以用其他形式的矩阵来代替。
>> reshape(1:15,5,3)
ans =
1 6 11
2 7 12
3 8 13
4 9 14
5 10 15
>> reshape(1:2:16,2,4)
ans =
1 5 9 13
3 7 11 15