Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
0

Given a matrix M how to form the following matrix N from M.

asked 5 years ago

Captcha gravatar image

Given a matrix M how to form the following matrix N from M.

Suppose M=[03 20]

Here M is a 2×2 matrix with 1st row [0,3] and 2nd row [2,0].

We need to form N such that

N= [33 22]

Here N is a 2×2 matrix with 1st row [3,3] and 2nd row [2,2].

Thus N is formed from M by just adding all the off the diagonal elements of M in a given row to the diagonal element

So the diagonal element of N is the sum of all the remaining entries in a given row of M whereas the rest of the elements of N are the same as M.

So the elements of N are obtained from M in the following way :

a11=0+3=3,a12=3,a21=2,a22=2+0=2

How to code it?

Please help.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 5 years ago

tmonteil gravatar image

updated 5 years ago

Given a matrix M, you can get its ith row with M.row(i), and the sum of its values with sum(M.row(i)). Now you just have to make a loop and modify diagonal elements step by step:

def myfunc(M):
    assert M.is_square()
    d,e = M.dimensions()
    N = copy(M)
    for i in range(d):
        N[i,i] = sum(N.row(i))
    return N

We have:

sage: M = matrix(2,2,[0,3,2,0])
sage: M
[0 3]
[2 0]
sage: myfunc(M)
[3 3]
[2 2]
Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 5 years ago

Seen: 250 times

Last updated: Aug 29 '19