Skip to content

converting matrix in array

3 messages · Marco, David Winsemius, William Dunlap

#
Hi everyone, 
have a small problem trying to converting a dataset in matrix form to an
array.
Specifically: data include 3D measurement -x,y,z of 59 points in 36 objects.
They are stored as a matrix (x) of 2124 rows and 3 columns.
What I want to do is to extract each subject's dataset using an array (b).
Accordingly, I tried the following command:

b<-array(a,c(59,3,36)).

The problem is that the resulting array for some strange reason change the
order of the original data.
Specifically, I noticed that, for example, in the first subject, the x y z
values of the first point are the x values of the first 3 subjects.

Did I perhaps missed something?
Thanks in advance



--
View this message in context: http://r.789695.n4.nabble.com/converting-matrix-in-array-tp3775025p3775025.html
Sent from the R help mailing list archive at Nabble.com.
#
On Aug 28, 2011, at 5:09 PM, marco wrote:

            
Not for a "strange reason". The 59 elements in b in column-1 would be  
the same in both , but you don't seem to realize that the 60th through  
the 118th elements in your original matrix were also all in the x- 
column. You have now effectively moved those values to what you were  
thinking to use as the y-column.
You should have kept the last dimension the same and redimension the  
object/points indexing by factoring the 2124 rows into what will  
become columns and row and leave the xyz coordinates "at the end". I  
think that redimensioning as b<-array(a,c(59, 36, 3)) would have re- 
folded it sensiblyly. So the b[n,m,] call would retrieve xyz- 
coordinates from the n-th point of the m-th object.
David Winsemius, MD
West Hartford, CT
#
It helps to consider a small self-containd example
where the correct answer is obvious.  Does the following
look like your input and desired output?

  > a <- rbind(c("obj1-ptA-x", "obj1-ptA-y", "obj1-ptA-z"),
  +            c("obj1-ptB-x", "obj1-ptB-y", "obj1-ptB-z"),
  +            c("obj2-ptA-x", "obj2-ptA-y", "obj2-ptA-z"),
  +            c("obj2-ptB-x", "obj2-ptB-y", "obj2-ptB-z"))
  > a
               [,1]         [,2]         [,3]
  [1,] "obj1-ptA-x" "obj1-ptA-y" "obj1-ptA-z"
  [2,] "obj1-ptB-x" "obj1-ptB-y" "obj1-ptB-z"
  [3,] "obj2-ptA-x" "obj2-ptA-y" "obj2-ptA-z"
  [4,] "obj2-ptB-x" "obj2-ptB-y" "obj2-ptB-z"
  > aperm(array(a, c(2, 2, 3)), c(1,3,2))
  , , 1
  
               [,1]         [,2]         [,3]
  [1,] "obj1-ptA-x" "obj1-ptA-y" "obj1-ptA-z"
  [2,] "obj1-ptB-x" "obj1-ptB-y" "obj1-ptB-z"
  
  , , 2
  
               [,1]         [,2]         [,3]
  [1,] "obj2-ptA-x" "obj2-ptA-y" "obj2-ptA-z"
  [2,] "obj2-ptB-x" "obj2-ptB-y" "obj2-ptB-z"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com