As you have noticed, the function that I suggested does only work for
symmetric matrices; here is a correction that works for all matrices:
foo <- function(x) {
n <- ncol(x)
xc <- matrix(1:n,n,n)
xr <- t(xc)
ind <- xr <= xc
x[,xr[ind]] * x[,xc[ind]]
}
There is no need for looping...
Gardar
On Fri, 15 Sep 2000, Pamela Goodman wrote:
>
> Greetings Everyone,
>
> I need to do a computation that requires all unique products of two
columns
> of a matrix (or data frame, I'm not picky), including the multiplication
of
> the columns with themselves.
>
> That is, for a 2x3 matrix:
>
> test <- matrix(1:6, nrow=2)
>
> I need:
>
> m1<-test[,1]*test[,1]
> m2<-test[,1]*test[,2]
> m3<-test[,1]*test[,3]
> m4<-test[,2]*test[,2]
> m5<-test[,2]*test[,3]
> m6<-test[,3]*test[,3]
>
> and products such as:
>
> m7<-test[,3]*test[,2]
>
> are redundant.
>
> The problem is that the data sets will dictate the number of columns,
and
> each data set will likely be different. I need to have the code generate
the
> correct number of products for the general case.
>
> Because this "all-possible-2-column-products" computation is going to be
> done at each iteration, until convergence, I would like the above done
> without looping.
>
> I will compile the answers I receive and post them to the group.
>
> Thanks in advance,
>
> Pam
>
>
> ###################################
> Pam Goodman
> Biometrician
> Northwest Indian Fisheries Commission
> 6730 Martin Way E
> Olympia, WA 98516-5540
> Phone: (360)438-1181 ext 380
> Fax: (360)753-8659
> http://www.nwifc.wa.gov/
> ###################################
> -----------------------------------------------------------------------
> This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
> send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
> message: unsubscribe s-news
>
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news
|