Skip to content

matrix dimension and for loop

11 messages · sonchawan tamkaew, Jonathan Baron, Douglas Bates +4 more

#
Dear all,

My questions are that if I have
1. Is there any other way to write this without 'for'
loop?

2. and if I don't know the dimension of x, which could
be only 1 column, how could I write the command
without using if (NCOL(x)==1), or something like that?
(in case I want to include them in my function.

Example of x is a vector.
Error in x[, j] : incorrect number of dimensions

Could anyone please help me to solve these problem? 
Thank you very much for your kindness.


Sonchawan Tamkaew 

__________________________________________________



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On 04/09/02 05:46, sonchawan tamkaew wrote:
x <- matrix(rnorm(50),10,5) # collapsing your first two commands
y <- 1:5
Z <- matrix(0,nrow(x),length(y)) # case sensitive, and y is a vector 
Z[,length(y)] <- x %*% y

It isn't clear that this is what you want, though.  It fills up
one column of Z.
I think it will still work.  So long as you pay attention to what
is a matrix - to which dim() applies - and what is a vector, to
which length() applies.
#
sonchawan tamkaew <psu17772 at yahoo.com> writes:
Yes.  If you multiply (i.e. the '*' operator) a matrix and a vector
you will do operations columnwise on the matrix.  Here you want to
multiply the rows so you could transpose x, multiply by y and
transpose the result.
[1] 10  5
[1] 10  5
[1]  0.23144399 -0.25393369  0.06469486 -0.13116405  0.48534804
[1]  0.2314440 -0.5078674  0.1940846 -0.5246562  2.4267402
The above will work in that case too.
[,1]
 [1,]  1.5755705
 [2,]  0.3811325
 [3,] -0.4629773
 [4,]  1.7612785
 [5,]  0.7881174
 [6,] -0.2815202
 [7,]  0.4315893
 [8,]  1.1609736
 [9,]  1.8737894
[10,]  5.3069982
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
sonchawan tamkaew wrote:
x %*% diag(y, ncol(x))

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Tue, Apr 09, 2002 at 05:46:40AM -0700, sonchawan tamkaew wrote:
Z <- x * y

seems to do the job....
still seems to do the job...
note: I discovered above that assignment like 'y=2' where now valid....
(learn something new evryday on r-help.... ;-) )




 where now valid....
(learn something new evryday on r-help.... ;-) )




L

  
    
#
Laurent Gautier <laurent at genome.cbs.dtu.dk> writes:
Nope. Try it with x <- matrix(1,10,5) and see.
#
On Tue, Apr 09, 2002 at 07:12:08PM +0200, Peter Dalgaard BSA wrote:
...right... it was 'y[j]' and not 'y[,j]' (blurry eyes... looking too long at the screen)


this is probably what everybody already gave:
(I receive r-help few hours before the first ones to receive it)


 x * matrix(y, ncol=ncol(x), nrow=nrow(x), byrow=T)


(I hope it work for good now... :)  )




L.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On 9 Apr 2002 at 5:46, sonchawan tamkaew wrote:

            
Z <- t( t(x) * y )
The answer already given to theprevious
question automatically handles the case 
where x is a vector so its not necessary to 
provide special code for it.

However, even though its not needed here,
just for interest:

x <- as.matrix( x ) 

will turn vector x into a 1 column matrix and leave it
as is if its already a matrix.


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
I thought I would collect together some of the solutions that have
been posted as well as mention a few others.  I have also added
some discussion particularly with reference to the second question.
There appears to be a myriad of ways to address this problem in R.
Since I am learning R I found it useful for myself to explore various
solutions and maybe others will find this helpful too.

The problem is to multiply the jth column of a matrix x by y[j] for each j.
Its also possible for x to be a vector in which case y may be a scalar.

The first 4 solutions regard x as a one column matrix if x is a vector and
so always return a matrix.  The 5th solution returns a vector if x is a 
vector.


1. Matrix multiplication. Note that if x is a vector then it is treated as a
   one column matrix.  Also note that NROW(x) is the same as nrow(x), if x is a
   matrix; however, if x is a vector, then NROW(x) is its length while nrow(x) 
   is NULL.  Thus it is important here to use NROW and not nrow. This is 
   discussed in the help on nrow.

	x %*% diag(y,NROW(x))  

2. Scalar multiplication and transpose.  Note that if x is a vector then t(x) 
   is a row matrix made out of it, so no special processing is necessary.  

	t(t(x)*y)  

3. Sweep.  Note that as.matrix(x) is the same as x, if x is a matrix, but is a 
   column matrix made out of x, if x is a vector.

	sweep(as.matrix(x),2,y,FUN="*")  

4. Scalar multiplication and col.  col(m) is a matrix the same size as
   matrix m.  Every entry in column j equals j (for all columns).

	as.matrix(x) * y[col(as.matrix(x))]   

5. Variant of #4. The following is the same as #4 except that it returns a 
   vector if x is a vector.

	x * y[col(as.matrix(x))]   


as.matrix(x) can be abbreviated to just x, in #3, #4 and #5 
if x is known always to be a matrix.  For example, the last two solutions 
would be  just x*y[col(x)].


I had not expected the last 2 solutions to work in the case that x is a vector
and y is a scalar, since they involve subscripting of a scalar, but apparently
that works in R.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
ggrothendieck at yifan.net wrote:
Not quite. The number of *columns* of x, ncol(x), is interesting here,
not NROW(x),
or just use length(y) instead of ncol(x) ....


Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On 10 Apr 2002 at 16:47, Uwe Ligges wrote:

            
Thanks for pointing out this error.  Note, however, that NROW(x) should
be NCOL(x), and not ncol(x), since x can be a vector.   length(y) is
probably best.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._