Any command in matlab equivilent to vech() in gauss

3 次查看(过去 30 天)
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

回答(1 个)

dpb
dpb 2016-11-21
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 个评论
Guillaume
Guillaume 2016-11-21
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
dpb 2016-11-21
编辑:dpb 2016-11-22
"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...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by