Search code examples
rmatrixextract

Extract several different elements of a matrix with one command?


if you have any matrix .. like for example the one attached. And you only want to extract the elements [1,2],[1,3],[1,4],[2,3],[2,4] and [3,4] ... is there any possibility to do this with only one command?

Thanks in advance.

enter image description here


Solution

  • If you are wanting to extract all the numbers above the diagonal (i.e., the 1.00000s), then you can use a different function without having to specify the positions. For above the diagonal, you can use upper.tri(); then for below, you can use lower.tri().

    m1[upper.tri(m1)]