matlab基础知识学习(四)

一、如何用matlab快速的建立多个空矩阵:

假设我要建立十个空矩阵:

>> a=cell(1,10)

a =

    []    []    []    []    []    []    []    []    []    []
填充应用这些空矩阵:

>> a{1,1}=ones(1,3)

a =

    [1x3 double]    []    []    []    []    []    []    []    []    []

二、如何从矩阵里找到满足某一条件的行向量或者列向量:

假设我们要找到一个矩阵最后一列为1的行向量:

>> A=[1 1 0;1 1 1;1 1 2;2 2 0]

A =

     1     1     0
     1     1     1
     1     1     2
     2     2     0
>> B=find(A(:,3)==1)

B =

     2

>> C=A(find(A(:,3)==1),:)

C =

     1     1     1

三、如何求两个向量的交集与并集:

>> A=[1 2 3 4 5];
>> B=[1 4];
>> C=intersect(A,B)


C =


     1     4


>> D=union(A,B)


D =


     1     2     3     4     5


四、随机产生数的方法:

(1)rand

rand(n)是随机生成0到1之间的随机n阶方阵;

rand(m,n)是随机生成的0到1之间的m*n的矩阵;

例如:


>> rand(3)


ans =


    0.8147    0.9134    0.2785
    0.9058    0.6324    0.5469
    0.1270    0.0975    0.9575


>> rand(3,2)


ans =


    0.9649    0.9572
    0.1576    0.4854
    0.9706    0.8003

(2)randint

randint(m,n,[1 N])随机生成1到N之间m*n矩阵;

>> randint(3,4,[1 100])
Warning: This is an obsolete function and may be removed in the future. Please use RANDI instead. 
> In randint at 41 


ans =


    15    80     4    68
    43    96    85    76
    92    66    94    75

(3)randperm

randperm(n)随机成成1到n的数据排序;

>> randperm(10)


ans =


     5     7     8     3     6     1     2    10     4     9

randperm实现的原理:

p=randperm(n)

 [ignore,p] = sort(rand(1,n))

>> [ignore,p] = sort(rand(1,10))


ignore =


  Columns 1 through 9


    0.1190    0.1626    0.2760    0.4984    0.6463    0.6551    0.6797    0.7094    0.7547


  Column 10


    0.9597


p =


     8     7     4     9     1     6     5     2     3    10

ignore是随机数重新由小到大排序的结果;p是排序以后各数的索引;

五、求矩阵的平均值

>> A=[1 1 1;2 2 2;3 3 3];
>> [m n]=size(A);
>> M=mean2(A);

%mean2用于求矩阵的平均值



你可能感兴趣的:(matlab)