对角矩阵

对角矩阵(diagonal):M是一个对角矩阵,则当且仅当i≠j时,M(i,j)=0。
一个rows×rows的对角矩阵D可以表示为 一个二维数组element[rows][rows],其中element[i-1][j-1]表示D(i,j)。
这种表示法需要rows²个数据类型为T的数据空间。
对角矩阵最多含有rows个非0元素,因此可以用一维数组element[rows]来表示对角矩阵,其中element[i-1]表示D(i,i)
所有未在一维数组中出现的矩阵元素均为0.这种表示法仅仅需要rows个类型为T的数据空间。 对角矩阵
diagonalMatrix.cpp

/*
 * 对角矩阵测试函数的主函数
 * diagonalMatrix.cpp
*/
#include<iostream>
#include"diagonalmatrix.h"

using namespace std;

int main(void)
{
    diagonalMatrix<int> x(20);
    x.set(1,1,22);
    x.set(5,5,44);
    x.set(8,5,0);

    cout<<x.get(5,5) <<endl;
    cout<<x.get(1,1) <<endl;
    cout<<x.get(10,1) <<endl;

    return 0;
}

diagonalMatrix.h

/*
 * 对角矩阵
 * diagonalMatrix.h
*/
#ifndef DIAGONALMATRIX_H
#define DIAGONALMATRIX_H

#include"myexceptions.h"

template<class T>
class diagonalMatrix
{
public:
    diagonalMatrix(int theN = 10);
    ~diagonalMatrix(){delete [] element;}
    T get(int,int) const;
    void set(int,int,const T&);
private:
    int n;
    T* element;
};

//构造函数的具体实现
template<class T>
diagonalMatrix<T>::diagonalMatrix(int theN)
{
    if(theN < 1)
        throw illegalParameterValue("Matrix size must be > 0");

    n = theN;
    element = new T [n];
}


//get()函数的具体实现
template<class T>
T diagonalMatrix<T>::get(int i, int j) const
{
    if(i < 1 || j < 1 || i > n || j > n)
        throw matrixIndexOutOfBounds();

    if(i == j)
        return element[i - 1];
    else
        return 0;
}

//set()函数的具体实现
template<class T>
void diagonalMatrix<T>::set(int i, int j, const T& newValue)
{
    if(i<1 || j < 1 || i > n || j > n)
        throw matrixIndexOutOfBounds();
    if(i == j)
        element[i-1] = newValue;
    else
        if(newValue != 0)
            throw illegalParameterValue
                ("Nondiagonal elements must be zero");
}

#endif // DIAGONALMATRIX_H

myExceptions.h

/*
 *异常类
 *myExceptions.h
*/
#ifndef MYEXCEPTIONS_H
#define MYEXCEPTIONS_H
#include<string>

using namespace std;


//不合法的参数值
class illegalParameterValue
{
public:
    illegalParameterValue(string theMessage = "Illegal parameter value")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout <<message<<endl;
    }
private:
    string message;
};

//数组索引不合法
class matrixIndexOutOfBounds
{
public:
    matrixIndexOutOfBounds(string theMessage = "Matrix index out of bounds")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout << message <<endl;
    }
private:
    string message;
};

//数组大小不匹配
class matrixSizeMismatch
{
public:
    matrixSizeMismatch(string theMessage =
            "The size of the two matrics doesn't match")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout << message <<endl;
    }
private:
    string message;
};


#endif // MYEXCEPTIONS_H

转载于:https://my.oschina.net/u/1771419/blog/1818059

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Matlab中,对角矩阵可以通过使用diag函数来构造。有两种常见的用法: 1. 提取矩阵的对角线元素:使用diag(A)可以提取矩阵A的主对角线元素,返回一个列向量。例如,如果A是一个4x4的矩阵,则diag(A)将返回一个4x1的列向量,包含A的主对角线上的元素。如果想要提取矩阵A的第k条对角线的元素,可以使用diag(A,k),其中k是一个整数。例如,diag(A,1)将返回A的第一条对角线上的元素。 2. 构造对角矩阵:使用diag(V)可以以向量V为主对角线元素创建一个对角矩阵。返回的矩阵将是一个二维矩阵,其主对角线上的元素由向量V的元素给定。例如,如果V=[1:5,则diag(V)将返回一个5x5的对角矩阵,其主对角线上的元素为1, 2, 3, 4, 5。如果想要创建以向量V为第k条对角线元素的对角矩阵,可以使用diag(V,k),其中k是一个整数。例如,diag(1:4,1)将返回一个5x5的对角矩阵,其第一条对角线上的元素为1, 2, 3, 4。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Matlab的对角阵、三角阵,矩阵变换:矩阵的转置、旋转、翻转、求逆、方阵的行列式、矩阵的秩求解](https://blog.csdn.net/HangHug_L/article/details/107944238)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值