constructing symatrical matrix out of vector

1 vue (au cours des 30 derniers jours)
Tim Elbers
Tim Elbers le 20 Mai 2019
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

Réponse acceptée

Jos (10584)
Jos (10584) le 21 Mai 2019
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

Plus de réponses (1)

Josh
Josh le 21 Mai 2019
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));

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by