Armadillo C++ linear algebra library 学习笔记(6)——生成矩阵

1、主对角线为1的矩阵

A、通过调用”eye(rows, cols)”函数生成主对角线为1的矩阵。
B、示例

#include 
#include 
using namespace arma;
int main()
{
    //生成大小为3x4,主对角线为1的矩阵
    mat B = eye(3,4);
    B.print("矩阵B:\n");

    system("pause");
    return 0;
}

C、结果
Armadillo C++ linear algebra library 学习笔记(6)——生成矩阵_第1张图片

2、把区间[a,b]分成n份

A、通过调用“linspace(start, end, n)”函数把区间[start,end]分成n份。
B、示例

#include 
#include 
using namespace arma;
int main()
{
    //把区间[a,b]分成n份,生成的是列向量。
    colvec colVec = linspace(1,10,20);//把[1,10]分成20份
    colVec.print("列向量colVec:\n");

    system("pause");
    return 0;
}

C、结果
Armadillo C++ linear algebra library 学习笔记(6)——生成矩阵_第2张图片

3、以矩阵为元素构造矩阵

A、通过调用“repmat(A, p, q)”函数构造矩阵,其元素为矩阵A,大小为pxq。
B、示例

#include 
#include 
using namespace arma;
int main()
{
    //以矩阵A整体为单位,生成大小为2x3的矩阵
    mat A = randu(3, 3)*10;
    A.print("矩阵A:\n");
    //矩阵D为:以矩阵A为元素的,大小为2x3的矩阵
    mat D = repmat(A,2,3);
    D.print("D:\n");

    system("pause");
    return 0;
}

C、结果
Armadillo C++ linear algebra library 学习笔记(6)——生成矩阵_第3张图片

4、生成托普利兹(Toeplitz)矩阵

A、通过调用”toeplitz(A, B)”函数生成托普利兹(Toeplitz)矩阵。
B、示例

#include 
#include 
using namespace arma;
int main()
{
    //生成托普利兹矩阵
    mat B = randu(5)*10;
    mat C = randu(5)*10;
    B.print("向量B:\n");
    C.print("向量C:\n");
    //矩阵E为托普利兹矩阵
    mat E = toeplitz(B,C);
    E.print("向量E:\n");

    system("pause");
    return 0;
}

C、结果
Armadillo C++ linear algebra library 学习笔记(6)——生成矩阵_第4张图片

你可能感兴趣的:(Armadillo)