Matlab基础_1

来源:互联网 发布:西门子数控编程软件 编辑:程序博客网 时间:2024/06/08 19:42
    1.
p=[2 3 4   3 4 5   1 2 3 ]sum1=cumsum(p,1)sum2=cumsum(p,2)

cumsum的第二个参数为1或者缺省时,依次计算列元素的和;第二个参数为2时,依次计算行元素的和。

p =     2     3     4     3     4     5     1     2     3sum1 =     2     3     4     5     7     9     6     9    12sum2 =     2     5     9     3     7    12     1     3     6
    2.
x=[1 0   0 5   1 2   3 4]find(x)find(x,5)% I = find(X,K,'first') is the same as I = find(X,K).find(x,5,'last')find(x>2)

find函数找到矩阵中非零的元素的索引(从第一列开始,到第二列······),第二个参数可选择要多少个非零元素的索引,例子中为5,就输出前5个非零元素的索引,第三个参数可选‘first’或‘last’,表示从前面开始数还是从后面开始数。参数为不等式时,表示输出矩阵中符合不等式的元素的索引。

x =     1     0     0     5     1     2     3     4ans =     1     3     4     6     7     8ans =     1     3     4     6     7ans =     3     4     6     7     8ans =     4     6     8
  1. randperm Random permutation.
    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].

    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].

  2. 4.