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

2 views (last 30 days)
Usman Mussadiq on 14 Sep 2021
Commented: Usman Mussadiq on 14 Sep 2021
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 on 14 Sep 2021
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

Awais Saeed on 14 Sep 2021
Edited: Awais Saeed on 14 Sep 2021
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
Usman Mussadiq on 14 Sep 2021
Thank You @Awais Saeed

KSSV on 14 Sep 2021
You need to proceed like this:
for i = 1:m
for j = 1:n
if i <= j
end
end
end
Jan on 14 Sep 2021
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 >= ?