Skip to content

extract half a matrix

9 messages · Emmanuel Paradis, Uwe Ligges, Roger Koenker +5 more

#
Dear all,

I'm new to matrix operations in R. I couln't find a solution to the
following problem among earlier help mails or in An introd to R, I guess
because the question is really basic.

I want to extract all above the diagonal, i.e. from 

    1  2  3  4 
1   0 26 49 49 
2  26  0 44 40 
3  49 44  0 21 
4  49 40 21  0 

I want 

26
49
44
49
40
21


Thanks in advance!


Sincerely,
Tord

-----------------------------------------------------------------------
Tord Sn?ll
Avd. f v?xtekologi, Evolutionsbiologiskt centrum, Uppsala universitet
Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University
Villav?gen 14			
SE-752 36 Uppsala, Sweden
Tel: 018-471 28 82 (int +46 18 471 28 82) (work)
Tel: 018-25 71 33 (int +46 18 25 71 33) (home)
Fax: 018-55 34 19 (int +46 18 55 34 19) (work)
E-mail: Tord.Snall at ebc.uu.se
Check this: http://www.vaxtbio.uu.se/resfold/snall.htm!
#
At 16:55 22/05/2003 +0200, vous avez ?crit:
If your matrix is named M, this should do it:

M[row(M) < col(M)]


EP
#
Tord Snall wrote:
X[row(X) < col(X)]

Uwe Ligges
#
A[row(A)<col(A)]


url:	www.econ.uiuc.edu	Roger Koenker		Dept. of Economics UCL,
email	rkoenker at uiuc.edu	Department of Economics Drayton House,
vox: 	217-333-4558		University of Illinois	30 Gordon St,
fax:   	217-244-6678		Champaign, IL 61820	London,WC1H 0AX, UK
							vox:	020-7679-5838
On Thu, 22 May 2003, Tord Snall wrote:

            
#
> A[col(A)>row(A)]
[1]  4  7  8 10 11 12

This is described in Modern Applied Statistics with S (p. 43 of the 3rd 
edition;  I'm pretty sure I've seen it in the 4th edition, but I don't 
have that handy just now.)

hth.  spencer graves
Tord Snall wrote:
#
Dear Tord,
At 04:55 PM 5/22/2003 +0200, Tord Snall wrote:

            
If you want the result as a one-column matrix, 
as.matrix(mat[upper.tri(mat)]) will do the trick.

John


-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
#
On Thu, 22 May 2003, Tord Snall wrote:

            
In addition to the solution suggested by other people there are built-in
functions upper.tri and lower.tri for doing this sort of thing, so

  x[upper.tri(x)]


	-thomas
#
Dear all,

Thanks for, as always the quick replies!

I have a suggestion: add the upper.tri() and lower.tri() under "See Also"
in the diag() help page. I searched for a such function there, and I can
see that diag() can be found unde "See also" for upper.tri().


Sincerely,
Tord
At 08:45 2003-05-22 -0700, Thomas Lumley wrote:
-----------------------------------------------------------------------
Tord Sn?ll
Avd. f v?xtekologi, Evolutionsbiologiskt centrum, Uppsala universitet
Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University
Villav?gen 14			
SE-752 36 Uppsala, Sweden
Tel: 018-471 28 82 (int +46 18 471 28 82) (work)
Tel: 018-25 71 33 (int +46 18 25 71 33) (home)
Fax: 018-55 34 19 (int +46 18 55 34 19) (work)
E-mail: Tord.Snall at ebc.uu.se
Check this: http://www.vaxtbio.uu.se/resfold/snall.htm!
#
On Thu, 22 May 2003 17:58:41 +0200, you wrote:

            
Good suggestion.  I've put it into r-patched, so it should be in 1.7.1
(which should be released on June 16, according to current plans.

Duncan Murdoch