Matrix manipulation: Changing Diagonal values

5 ビュー (過去 30 日間)
Abbey
Abbey 2017 年 2 月 26 日
コメント済み: Abbey 2017 年 2 月 26 日
Hello Everyone!
I have a matrix data which is from a nxn uitable inputted by a certain user, using this short code
S = get(handles.Table,'data');
I have successfully saved the data in the variable S.
My next goal is to create another matrix out of this matrix. My general formula is
NewMatrix(1,1) = S(1,1)-S(1,2)-S(1,3),...S(1,n)
NewMatrix(2,2) = S(2,2)-S(2,1)-S(2,3),...S(2,n)
So on and so forth. Yes, it is the diagonal element of the matrix which I would like to manipulat. And I wish to multiply by (-1) all of the remaining values in the S matrix.
The NewMatrix will have different diagonal values while all of the other data is the negative of S Matrix.
I hope someone could help me. Cause I am starting to get cray on this. Thank you.

採用された回答

Stephen23
Stephen23 2017 年 2 月 26 日
編集済み: Stephen23 2017 年 2 月 26 日
>> S = randi(9,3,5)
S =
4 3 9 4 4
6 4 2 2 9
6 1 1 5 9
>> idx = 1==eye(size(S));
>> new = -S;
>> new(idx) = sum(new,2) + 2*S(idx)
new =
-16 -3 -9 -4 -4
-6 -15 -2 -2 -9
-6 -1 -20 -5 -9
And checking:
NewMatrix(2,2) = S(2,2)-S(2,1)-S(2,3),...S(2,n)
>> S(2,2)-S(2,1)-S(2,3)-S(2,4)-S(2,5)
ans =
-15
>> new(2,2)
ans =
-15
  1 件のコメント
Abbey
Abbey 2017 年 2 月 26 日
Thank you so much!

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 2 月 26 日
-1 * (S - diag(diag(S))) + diag(NewValuesToInsert)
Or more compactly but less readable,
diag(diag(S)) - S + diag(NewValuesToInsert)
diag(diag(S)) first extracts the diagonal of S to a vector for the first diag call, and then the second call makes that vector the diagonal of a full matrix, with the end result that you have a matrix that is just the diagonal.
  1 件のコメント
Abbey
Abbey 2017 年 2 月 26 日
Thank you sir, however I am having difficulty using this in a nxn matrix.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by