[19076] in s-news-athena

home help back first fref pref prev next nref lref last post

[S] Putting values into matrices: desired behaviour?

daemon@ATHENA.MIT.EDU (Mette Langaas)
Wed Sep 1 02:38:40 1999

Message-Id: <37CCC8DE.A756F3D6@nr.no>
Date: Wed, 01 Sep 1999 08:34:06 +0200
From: Mette Langaas <Mette.Langaas@nr.no>
Reply-To: Mette.Langaas@nr.no
Mime-Version: 1.0
To: "s-news@wubios.wustl.edu" <s-news@wubios.wustl.edu>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit


S-Plus users and experts, 

I'm running S-Plus 5.1 on UNIX (but the same behaviour I'm describing is
seen on S-Plus 3.4 R1 for UNIX).

I have first created a matrix with two rows and three columns

> test_matrix(ncol=3,nrow=2)
> test
     [,1] [,2] [,3] 
[1,]   NA   NA   NA
[2,]   NA   NA   NA

I then want to put values into parts of the matrix, but by mistake I
index outside the matrix (use rows 3-7 when there are only 2 rows).

> ind_matrix(c(1,2,3,4,5,6,7,2,2,2,2,2,2,2),ncol=2)
> ind
     [,1] [,2] 
[1,]    1    2
[2,]    2    2
[3,]    3    2
[4,]    4    2
[5,]    5    2
[6,]    6    2
[7,]    7    2
> test[ind]_c(3,4,5,6,7,8,9)
> test
     [,1] [,2] [,3] 
[1,]   NA    3    5
[2,]   NA    4    6

The first observation is that in addition to correctly putting values 3
ang 4 into position (1,2) and (2,2) the value 
5 is put into (1,3) instead of (3,2) as requested and
6 is put into (2,3) instead of (4,3) as requested.

In addition the values 7,8,9 have also been put "somewhere" in the
matrix -- where I can not see them -- unless a convert the matrix into a
vector....

> dim(test)
[1] 2 3

> length(test)
[1] 9
> as.vector(test)
[1] NA NA  3  4  5  6  7  8  9

This new matrix "test" has length=9 which is neq
dim(test)[1]*dim(test)[2]. 

No error message is produced when I do my test[ind], but when I
try to use test I get into problems, e.g. with t(test):

> t(test)
Problem in t.default(test): length must equal product of dimensions 
Use traceback() to see the call stack

Is this the desired behaviour of indexing matrices? What measures can I
take to avoid this behaviour -- e.g. to instead get an error message?

Best regards
Mette Langaas
Norwegian Computing Center
-----------------------------------------------------------------------
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

home help back first fref pref prev next nref lref last post