仅向矩阵的对角元素添加一个小值 [英] adding a small value to only the diagonal elements of a matrix

查看:145
本文介绍了仅向矩阵的对角元素添加一个小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是matlab的新手,我正在尝试找出值非常小的矩阵逆.当我尝试找到逆时,我得到一个错误,说矩阵是奇异的.建议的解决方案之一是尝试向对角线元素添加一些元素.我知道我必须使用眼图和诊断方法,但是我无法提出正确的解决方案.

I am a newbie to matlab and I am trying to find out the inverse of matrix with very small values. When i try to find the inverse I get an error saying that the matrix is singular. One of the solutions suggested is to try and add some elements to the diagonal elements. I know i have to use eye and diag methods but I am not able come up the correct soltion.

任何评论都会有所帮助.

Any comments will be helpful.

推荐答案

如果您只想将身份矩阵或其倍数添加到 square 矩阵中,则可以进行

If you just want to add the identity matrix or a multiple of it to your square matrix, you can do

A_new = A_old + k*eye(size(A_old));

其中A_old是您的矩阵,k是一些乘数.如果您想为每个对角线元素添加不同的值,则可以执行类似的操作

where A_old is your matrix and k is some multiplier. If you want to add a different values to each diagonal element, you can do something like

A_new = A_old + diag(values);

其中values是一个向量,其元素数与矩阵A_old的列(或行)数一样.

where values is a vector with as many number of elements as the number of columns (or rows) of your matrix A_old.

如果您的矩阵很大,则将spdiags用作:

If your matrix is large, it will be more memory efficient to use spdiags as:

dim_A = size(A_old,1);
A_new = A_old + spdiags(values(:),0,dim_A,dim_A);

或像Amro的答案一样使用线性索引.

or use linear indexing like in Amro's answer.

这篇关于仅向矩阵的对角元素添加一个小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆