Any command in matlab equivilent to vech() in gauss
3 views (last 30 days)
Show older comments
The definition of vech() in Gauss is
vech
Purpose Vectorizes a symmetric matrix by retaining only the lower triangular portion of the matrix.
Format v = vech(x);
Input
Output
Remarks As you can see from the example below, vech will not check to see if x is symmetric. It just packs the lower triangular portion of the matrix into a column vector in row-wise order.
Example x = seqa(10,10,3) + seqa(1,1,3)’;
v = vech(x);
sx = xpnd(v);
x NxN symmetric matrix.
v (N*(N+1)/2)x1 vector, the lower triangular portion of the matrix
x
11 12 13
21 22 23
31 32 33
=
v
11
21
22
31
32
33
Something like
mask = tril(true(size(a)),0);
out = a(mask);
can only return like
11
21
31
22
32
33
0 Comments
Answers (1)
dpb
on 21 Nov 2016
It's simply row-major vis a vis column-major Matlab storage order --
>> triu(x')
ans =
11 21 31
0 22 32
0 0 33
>> ans(ans>0)
ans =
11
21
22
31
32
33
>>
2 Comments
Guillaume
on 21 Nov 2016
or as a one liner:
nonzeros(triu(x'))
Both assume that there are no 0 in the original matrix. So, this may be safer:
transx= x'
transx(triu(true(size(transx))))
dpb
on 21 Nov 2016
Edited: dpb
on 22 Nov 2016
"assume that there are no 0 in the original matrix"
Trudat; was only showing OP really the only difference is internal storage order.
With your refinement on selection of values to keep, it probably would be clearer to write
tril(x).'
as the selection which at least does use tril instead of triu the latter of which is undoubtedly disconcerting.. :)
It would take some playing, however, to get the testing to match for the original array vis a vis the transposed triangular one; not sure a one-liner is then possible altho I didn't work on it too long...
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!