Re: [R] proportional matrix rows
Hi Peter, Gabor On Feb 7, 2005, at 04:42 pm, Peter Dalgaard wrote: Gabor Grothendieck <[EMAIL PROTECTED]> writes: Robin Hankin soc.soton.ac.uk> writes: : I want a diagnostic that detects whether a row is a multiple of : the first row or not. ..and in that case it might be easier just to calculate the dot product with the orthogonal of mat[1,]: z <- jj[1,] jj %*% c(z[1], -z[2]) [,1] [1,]0 [2,]0 [3,]1 [4,]1 [5,]1 [6,]1 [7,]1 [8,]0 [9,]1 [10,]1 (Notice that the value "1" just comes from Robin being singularly bad at selecting "arbitrary" rows that are not proportional to the first one!) Heh. I spent some considerable time pondering this and realized that if one views jj as a bunch of two-by two matrices jj[c(a,b),] then in my application, many of these matrices are either singular or unimodular (that is, a matrix with a determinant of one). Your insight above is is VERY VERY helpful Thank you. -- Robin Hankin Uncertainty Analyst Southampton Oceanography Centre European Way, Southampton SO14 3ZH, UK tel 023-8059-7743 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] proportional matrix rows - with attachment
Sorry! Something went wrong with the attachment in my previous post. Here it is. --- intmult <- function(X) { # extract columns with non-zero top cell X1 <- X[,which(X[1,]!=0)] # look for integer multiples in reduced table m <- nrow(X1);n <- ncol(X1) D <- matrix(X1[1,],m,n,byrow=T) X2 <- X1/D ind <- which(rowSums(X2==rowMeans(X2))==n) # build new table of integer multiples X3 <- X[ind,] # remove rows that contain non-zero(s) below top zero ind <- which(X3[1,]==0) lind = length(ind) temp <- X3[,ind]==0 ind2 <- which(rowSums(temp)==lind) X4 <- X3[ind2,] } -- No virus found in this outgoing message. Checked by AVG Anti-Virus. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] proportional matrix rows
Robin The attached script works but it ain't pretty/ Martin -- No virus found in this outgoing message. Checked by AVG Anti-Virus. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] proportional matrix rows
Gabor Grothendieck <[EMAIL PROTECTED]> writes: > Robin Hankin soc.soton.ac.uk> writes: > > : > : Hi > : > : I have a two-column integer matrix like this: > : > : R> jj > : > :[,1] [,2] > : [1,] -11 > : [2,] -22 > : [3,] -76 > : [4,] -87 > : [5,] -65 > : [6,] -98 > : [7,] -54 > : [8,]3 -3 > : [9,] -109 > : [10,] -43 > : > : I want a diagnostic that detects whether a row is a multiple of > : the first row or not. In this case, this would be rows 1,2, and 8. > : > : How to do this nicely? Sometimes the first row has a zero, so the > : method would have to work on > : > : [,1] [,2] > : [1,]01 > : [2,]11 > : [3,]08 > : [4,]0 -4 > : [5,]00 > : [6,]40 > : > > : > : in which case rows 1,3,4,5 are multiples. It'd be nice to have > : a solution that works for any number of columns. > > > If rowi is a multiple of row1 then > crossprod(cbind(row1, rowi)) is singular so: > > apply(mat, 1, function(x) det(crossprod(cbind(x, mat[1,] == 0 > > If your matrix only has two columns then crossprod(x) is singular > iff x is so you can eliminate the crossprod. ..and in that case it might be easier just to calculate the dot product with the orthogonal of mat[1,]: > z <- jj[1,] > jj %*% c(z[1], -z[2]) [,1] [1,]0 [2,]0 [3,]1 [4,]1 [5,]1 [6,]1 [7,]1 [8,]0 [9,]1 [10,]1 (Notice that the value "1" just comes from Robin being singularly bad at selecting "arbitrary" rows that are not proportional to the first one!) This generalizes to more than two columns as jj %*% (diag(ncol(jj)) - z%*%solve(crossprod(z))%*%t(z)) having rows that are identically zero (the multiplier matrix is obviously rank-deficient so you can leave out one column, but you need to guard against leaving in a column of zeroes, so it is hardly worth it). In all cases you probably need some sort of guard against round-off. I.e. something like > rowSums(zapsmall(jj %*% (diag(2) - z%*%solve(crossprod(z),t(z^2) == 0 [1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE or > rowSums((jj %*% (diag(2) - z%*%solve(crossprod(z),t(z^2) < 1e-7 [1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] proportional matrix rows
This works for your first example: f <- function(m) { k <- t(t(m) %/% m[1,]) which(rowSums(k - k[, 1]) == 0) } > f(jj) [1] 1 2 8 but not the second one. The 0s are problematic... Andy > From: Robin Hankin > > Hi > > I have a two-column integer matrix like this: > > > R> jj > >[,1] [,2] > [1,] -11 > [2,] -22 > [3,] -76 > [4,] -87 > [5,] -65 > [6,] -98 > [7,] -54 > [8,]3 -3 > [9,] -109 > [10,] -43 > > I want a diagnostic that detects whether a row is a multiple of > the first row or not. In this case, this would be rows 1,2, and 8. > > How to do this nicely? Sometimes the first row has a zero, so the > method would have to work on > > [,1] [,2] > [1,]01 > [2,]11 > [3,]08 > [4,]0 -4 > [5,]00 > [6,]40 > > > > in which case rows 1,3,4,5 are multiples. It'd be nice to have > a solution that works for any number of columns. > > -- > Robin Hankin > Uncertainty Analyst > Southampton Oceanography Centre > European Way, Southampton SO14 3ZH, UK > tel 023-8059-7743 > > __ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] proportional matrix rows
Robin Hankin soc.soton.ac.uk> writes: : : Hi : : I have a two-column integer matrix like this: : : R> jj : :[,1] [,2] : [1,] -11 : [2,] -22 : [3,] -76 : [4,] -87 : [5,] -65 : [6,] -98 : [7,] -54 : [8,]3 -3 : [9,] -109 : [10,] -43 : : I want a diagnostic that detects whether a row is a multiple of : the first row or not. In this case, this would be rows 1,2, and 8. : : How to do this nicely? Sometimes the first row has a zero, so the : method would have to work on : : [,1] [,2] : [1,]01 : [2,]11 : [3,]08 : [4,]0 -4 : [5,]00 : [6,]40 : > : : in which case rows 1,3,4,5 are multiples. It'd be nice to have : a solution that works for any number of columns. If rowi is a multiple of row1 then crossprod(cbind(row1, rowi)) is singular so: apply(mat, 1, function(x) det(crossprod(cbind(x, mat[1,] == 0 If your matrix only has two columns then crossprod(x) is singular iff x is so you can eliminate the crossprod. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] proportional matrix rows
Hi I have a two-column integer matrix like this: R> jj [,1] [,2] [1,] -11 [2,] -22 [3,] -76 [4,] -87 [5,] -65 [6,] -98 [7,] -54 [8,]3 -3 [9,] -109 [10,] -43 I want a diagnostic that detects whether a row is a multiple of the first row or not. In this case, this would be rows 1,2, and 8. How to do this nicely? Sometimes the first row has a zero, so the method would have to work on [,1] [,2] [1,]01 [2,]11 [3,]08 [4,]0 -4 [5,]00 [6,]40 > in which case rows 1,3,4,5 are multiples. It'd be nice to have a solution that works for any number of columns. -- Robin Hankin Uncertainty Analyst Southampton Oceanography Centre European Way, Southampton SO14 3ZH, UK tel 023-8059-7743 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html