How to create a matrix with 1 on ij-th position and zeros elsewhere from a lower triangular matrix?

조회 수: 3 (최근 30일)
It may be a very simple question For a symmetric matrix A (3x3), say A=[2 4 6;4 8 11;6 11 20], the way to extract its unique elements (on and lower the diagonal) in an output vector B is:
B=(A(tril(A)~=0))
B = 2
4
6
8
11
20
How can I create matrices C1,C2,C3,...,C6, such that
B(1)=A.*C1, B(2)=A.*C2, ..., B(6)=A.*C6
C1=[1 0 0;0 0 0;0 0 0];
C2=[0 0 0;1 0 0;0 0 0];
C3=[0 0 0;0 0 0;1 0 0];
C4=[0 0 0;0 1 0;0 0 0];
C5=[0 0 0;0 0 0;0 1 0];
C6=[0 0 0;0 0 0;0 0 1];
  댓글 수: 4
Stephen23
Stephen23 2018년 5월 31일
편집: Stephen23 2018년 5월 31일
Try this:
[I,J] = find(tril(A)~=0)
Note that you can already find this in my answer, on this line:
[R,C] = find(T);
Niveen El Zayat
Niveen El Zayat 2018년 5월 31일
Stephen, my last comment answer my last question I add, the relation compute the index (i,j) corresponds to f. which as you comment it coincide with a part of you answer as well.
Thanks a lot for your prompt reply
Best regards

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

채택된 답변

Stephen23
Stephen23 2018년 5월 31일
편집: Stephen23 2018년 5월 31일
A = [2,4,6;4,8,11;6,11,20]
S = size(A);
T = tril(true(S));
[R,C] = find(T);
N = nnz(T);
Z = zeros([S,N]);
V = 1:N;
Z(sub2ind([S,N],R,C,V(:))) = 1
Each page of Z (i.e. the third dimension) is one of the requested matrices, which you can access easily using indexing:
>> Z(:,:,1)
ans =
1 0 0
0 0 0
0 0 0
>> Z(:,:,2)
ans =
0 0 0
1 0 0
0 0 0
...
>> Z(:,:,6)
ans =
0 0 0
0 0 0
0 0 1

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by