如何访问矩阵的对角线并更改值? [英] How to access the diagonals of a matrix and change the value?

查看:95
本文介绍了如何访问矩阵的对角线并更改值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有8x8矩阵,其中有15个对角线.左上角对角1",下 右上角对角线15".我想将对角线的特定集合清零,例如{9、10、11、12、13、14、15}或{5、6、7、8、9、10、11、12、13、14、15} .有人请给我解决方法吗?

I have 8x8 matrix where I have 15 diagonals. upper left corner "diagonal 1" and the lower right corner "diagonal 15". I want to zero out the specific sets of diagonals like {9, 10, 11, 12, 13, 14, 15} or {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}. Anyone please give me solution?

推荐答案

我相信您可以使用:

M = M - diag(diag(M,k),k);

其中,k为主要对角线,k为负,较低的对角线(最多-7),正为较高的对角线(最多7).

where k is 0 for the main diagonal, negative for the lower diagonals (up to -7), positive for the upper diagonals (up to 7).

不好意思,这仅是您选择的对角线之一.您可以对要归零的所有对角线重复此过程,但这可能不是最佳选择:

My bad, this only zero out one of the diagonal of your choice. You can repeat the process for all the diagonals you want to zero out but that might not be optimal:

for k=[9 10 11 12 13 14 15]
    M = M - diag(diag(M,k-length(M)),k-length(M));
end

这篇关于如何访问矩阵的对角线并更改值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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