How to Copy Upper diagonal elements of matrix A into a new matrix.

20 ビュー (過去 30 日間)
Usman Mussadiq
Usman Mussadiq 2021 年 9 月 14 日
コメント済み: Usman Mussadiq 2021 年 9 月 14 日
A= 1 2 3 4; 2 1 3 4; 1 1 1 2; 1 0 0 1 Then how to copy the upper diagonal elements into a new Matrix using for loops etc.

採用された回答

Jan
Jan 2021 年 9 月 14 日
A = [1 2 3 4; 2 1 3 4; 1 1 1 2; 1 0 0 1];
B = triu(A)
B = 4×4
1 2 3 4 0 1 3 4 0 0 1 2 0 0 0 1

その他の回答 (2 件)

Awais Saeed
Awais Saeed 2021 年 9 月 14 日
編集済み: Awais Saeed 2021 年 9 月 14 日
A= [1 2 3 4; 2 1 3 4; 1 1 1 2; 1 0 0 1]
A = 4×4
1 2 3 4 2 1 3 4 1 1 1 2 1 0 0 1
M = zeros(size(A));
for row = 1:1:size(A,1)
for col = row:1:size(A,2)
M(row,col) = A(row,col);
end
end
disp(M)
1 2 3 4 0 1 3 4 0 0 1 2 0 0 0 1
  4 件のコメント
Awais Saeed
Awais Saeed 2021 年 9 月 14 日
I answered exactly as per your question "how to copy the upper diagonal elements into a new Matrix using for loops".
Why do you care how your developer is doing things. If you think he is making a mistake or he needs help in doing what you asked in your question then just simply show him the proposed answer. If he does not like doing it in a For Loop then use triu(A) as @Jan have mentioned.
Usman Mussadiq
Usman Mussadiq 2021 年 9 月 14 日
Thank You @Awais Saeed

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


KSSV
KSSV 2021 年 9 月 14 日
You need to proceed like this:
for i = 1:m
for j = 1:n
if i <= j
end
end
end
  6 件のコメント
Usman Mussadiq
Usman Mussadiq 2021 年 9 月 14 日
@Jan it is mentioned by the algo developer that he is copying the upper diagonal elements into temp, but in my understanding this code is generating a full matrix from the upper triangular elements only. Please guide me.
Jan
Jan 2021 年 9 月 14 日
I do not know what "copy into temp" means.
Maybe you want to get a vector with only the elements of the upper triangular matrix?
mask = (1:4) > (1:4).'; % Or >= ?
temp = A(mask)

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by