constructing symatrical matrix out of vector

조회 수: 1 (최근 30일)
Tim Elbers
Tim Elbers 2019년 5월 20일
답변: Jos (10584) 2019년 5월 21일
Hello all,
I have a question,
I have a vector that is previously vectorized out of a symatrical matrix and has the following form. (lengt 1 x L(L+1)/2 )
v = 1, 2*6, 2*7, 2*8, 2*9, 2, 2*10, 2*1,1 2*12, 3, 2*13, 2*14, 4, 2*15, 5
I now want to obtain the same matrix as before (Matrix H) of the underneath form (lengt L x L). How can I obtain this matrix out of above vector?
H =
1 6 7 8 9
6 2 10 11 12
7 10 3 13 14
8 11 13 4 15
9 12 14 15 5
thanks in advance

채택된 답변

Jos (10584)
Jos (10584) 2019년 5월 21일
v = [1, 2*6, 2*7, 2*8, 2*9, 2, 2*10, 2*11 2*12, 3, 2*13, 2*14, 4, 2*15, 5]
% |
% I assume the comma here in your example was a typo
m = tril(ones(5))
m(m==1) = v
m = (m + m.')/2

추가 답변 (1개)

Josh
Josh 2019년 5월 21일
I'm a little fuzzy on what v is supposed to be (are the off-diagonal elements of H stored in v all multiplied by 2?), so I'll solve the problem in two steps:
First, the indexing problem, how to reinflate v into H:
% Define inputs
v = [1, 6, 7, 8, 9, 2, 10, 11, 12, 3, 13, 14, 4, 15, 5];
L = (sqrt(1 + 8 * numel(v)) - 1)/2;
% Create an index matrix, I, such that v(I) give us H:
% Create a matrix of indices:
I = reshape(1:L*L, L, L);
% Duplicate the indices of the lower half into the upper half:
I = min(I, I');
% Re-index I to remove unused indices:
[~, ~, purged] = unique(I(:));
I = reshape(purged, size(I));
% Calculate the expanded matrix, H:
H = v(I);
If you do need to divide the off diagonal elements by 2:
H = v(I) ./ (2 - eye(L));

카테고리

Help CenterFile Exchange에서 Dynamic System Models에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by