本文介绍了增加值,使用MATLAB中逐元素除了矩阵对角线的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的矩阵运行的脚本,我遇到了需要到previous矩阵对角线的总和添加到新的矩阵对角线元素的问题。的code口到目前为止对于该特定功能(在下面更详细描述)是:

I am writing a script that operates on matrices, and I have run into the problem of needing to add the sum of the diagonals of a previous matrix to the diagonal elements of a new matrix. The code I have so far for this particular function (described in more detail below) is:

t = 1;
for k = (m-1):-1:-(m-1)
    C = bsxfun(@plus, diag(B, k), d);
    g(t) = sum(diag(B, k));
    t = t + 1;
end

其中d是一个1×3阵列,和C被认为是一个3×3阵列;然而,C正在作为以这样的方式,第一对角被相加并加入对d的1×3阵列输出,则主对角线被相加并添加至d,并最终对角被相加并添加至d。

where d is a 1x3 array, and C is supposed to be a 3x3 array; however, C is being output as a 1x3 array in such a way that the first diagonal is being summed and added to d, then the main diagonal is being summed and added to d, and the final diagonal is being summed and added to d.

有没有一种方法,我可以得到C的值是这样的,第一对角是它的总和的加入到D的最后一个元素各个元素,主对角线的单个元素加入到D的中间元素,底部对角线的元素加到D的第一个元素? (仍然工作的数组的大小?)

Is there a way I can get the values of C to be such that the first diagonal is the sum of it's individual elements added to the last element of d, the main diagonal's individual elements added to the middle element of d, and the bottom diagonal's elements added to the first element of d? (while still working for any array size?)

下面是图片,描述我想要实现的:

Here is a picture that describes what I'm trying to achieve:

谢谢!

推荐答案

您可以使用的生成包含需要被添加到您的原矩阵的值的矩阵:

You can use toeplitz to generate a matrix containing the values that need to be added to your original matrix:

M = [5 5 5; 7 7 7; 9 9 9]; %// data matrix
v = [1 11 4 3 2]; %// data vector
S = toeplitz(v);
S = S(1:(numel(v)+1)/2, (numel(v)+1)/2:end);
result = M+S;

或者,正如@thewaywewalk指出,可以更直接地做到这一点如下:

Or, as noted by @thewaywewalk, you can do this more directly as follows:

M = [5 5 5; 7 7 7; 9 9 9]; %// data matrix
v = [1 11 4 3 2]; %// data vector
result = M + toeplitz(v(size(M,1):-1:1), v(size(M,2):end));

这篇关于增加值,使用MATLAB中逐元素除了矩阵对角线的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:16