changing diagional values with different values

Hello,
Is there any way to do the following:
let assume this is my matrix (MX1):
x1 8 4 0
4 x2 0 9
1 0 x3 3
0 3 0 x4
what I'm trying to do is the followin:
I should calculate each "x", where x = the summation of the values in the same row. for Exp: X1 = 8+4+0. [are there any way to automate this calculation?]
My idea is changing the x's diagonal to zeros to get the summation from, where the x will be zero. [I know how to do it]
then, generating a duplicated matrix and copy the calculated diagonal to the new matrix. [Is that possible ?] *this is must be automated because my matrix is 98*98
Thanks

 채택된 답변

Star Strider
Star Strider 2019년 3월 21일

1 개 추천

I am not certain what result you want.
Try this:
MX1 = randi(9, 4)
D1 = diag(MX1);
Xv = sum(MX1,2) - D1;
MX1 = triu(MX1,1) + tril(MX1,-1) + diag(Xv)
Experiment to get the result you want.

댓글 수: 2

A Aln
A Aln 2019년 3월 21일
thank you so much, it works !!
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2019년 3월 21일

0 개 추천

n=5;
a=magic(n);
b=a;
index=sub2ind(size(a),1:n,1:n);
b(index)=0;
s=sum(b,2);
b(index)=s;

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2019년 3월 21일

댓글:

2019년 3월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by