convert vector to matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
i have a vector
a=[1,2,3,4,5,6];
i want to convert it to matrix such that the diagonals are all 0 and the rest of the elements are:
M=[0 1 2 3;1 0 4 5;2 4 0 6;3 5 6 0];
0 1 2 3
1 0 4 5
2 4 0 6
3 5 6 0
댓글 수: 0
채택된 답변
Stephen23
2018년 7월 3일
편집: Stephen23
2018년 7월 3일
>> a = [1,2,3,4,5,6];
>> M = tril(ones(4),-1);
>> M(M>0) = a;
>> M = M+M.'
M =
0 1 2 3
1 0 4 5
2 4 0 6
3 5 6 0
And if you want to automatically adjust the size you can basically invert the binomial coefficient:
>> N = (sqrt(1+numel(a)*8)-1)/2;
>> M = tril(ones(N+1),-1);
댓글 수: 1
Rik
2018년 7월 3일
If you need to automatically find the correct size:
a = [1,2,3,4,5,6];
M = tril(ones(.5+0.5*sqrt(8*numel(a)+1)),-1);
M(M>0) = a;
M = M+M.';
>> a = 1:3;
>> M = tril(ones(.5+0.5*sqrt(8*numel(a)+1)),-1);
>> M(M>0) = a;
>> M = M+M.'
M =
0 1 2
1 0 3
2 3 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Electrical Block Libraries에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!