Hi Richard,
> ## for an array with
> ## dim(a) == c(3,4,2)
> ## a[i,j,k] means select the element in position
> ## i + (j-1)*3 + (k-1)*3*4
My understanding;
e.g.
1)
dim(a) == c(3,4,2)
3 + (4-1)*3 + (2-1)*3*4
3+9+12=24
2)
## dim(a) == c(1,2,1)
1 + (2-1)*3 + (1-1)*3*4
1+3+0=4
3)
## dim(a) == c(2,3,1)
2 + (3-1)*3 + (1-1)*3*4
2+6+0=8
etc.
It is NOT always the product of i*j*k as I thought before. Thanks for your
explanation.
What are the value of 3 and 4? The values of position and dimension?
e.g.
> a <- sample(24)
> a
[1] 22 18 17 10 24 1 11 13 9 19 20 8 2 21 23 16 7 14 12 15 4 5 3 6
> dim(a) <- c(3,4,2)
> a
, , 1
[,1] [,2] [,3] [,4] <-- positions 1, 2, 3, 4 of the second dimension ?
[1,] 22 10 11 19
[2,] 18 24 13 20
[3,] 17 1 9 8
^ the first dimension ?
, , 2
[,1] [,2] [,3] [,4]
[1,] 2 16 12 5
[2,] 21 7 15 3
[3,] 23 14 4 6
? If I'm wrong pls correct me. TIA
Now I'm going to digest Joshua's advice.
B.R.
Stephen L
________________________________
From: RICHARD M. HEIBERGER <[email protected]>
Cc: Daniel Nordlund <[email protected]>; [email protected]
Sent: Sat, November 6, 2010 12:48:35 AM
Subject: Re: [R] About 5.1 Arrays
Continuing with Daniel's example, but with different data values
a <- sample(24)
a
dim(a) <- c(3,4,2)
a
as.vector(a)
## for an array with
## dim(a) == c(3,4,2)
## a[i,j,k] means select the element in position
## i + (j-1)*3 + (k-1)*3*4
index <- function(i,j,k) {
i + (j-1)*3 + (k-1)*3*4
}
## find the vector position described by row 2, column 1, layer 2
index(2,1,2) ## this is the position in the original vector
a[2,1,2] ## this is the value in that position with 3D indexing
a[index(2,1,2)] ## this is the same value with 1D vector indexing
a[14] ## this is the same value with 1D vector indexing
## find the position in row 3, column 4, layer 1
index(3,4,1) ## this is the position in the original vector
a[3,4,1] ## this is the value in that position with 3D indexing
a[index(3,4,1)] ## this is the same value with 1D vector indexing
a[12] ## this is the same value with 1D vector indexing
index(1,1,1) ## this is the position in the original vector
index(2,1,1) ## this is the position in the original vector
index(3,1,1) ## this is the position in the original vector
index(1,2,1) ## this is the position in the original vector
index(2,2,1) ## this is the position in the original vector
index(3,2,1) ## this is the position in the original vector
index(1,3,1) ## this is the position in the original vector
index(2,3,1) ## this is the position in the original vector
index(3,3,1) ## this is the position in the original vector
index(1,4,1) ## this is the position in the original vector
index(2,4,1) ## this is the position in the original vector
index(3,4,1) ## this is the position in the original vector
index(1,1,2) ## this is the position in the original vector
index(2,1,2) ## this is the position in the original vector
index(3,1,2) ## this is the position in the original vector
index(1,2,2) ## this is the position in the original vector
index(2,2,2) ## this is the position in the original vector
index(3,2,2) ## this is the position in the original vector
index(1,3,2) ## this is the position in the original vector
index(2,3,2) ## this is the position in the original vector
index(3,3,2) ## this is the position in the original vector
index(1,4,2) ## this is the position in the original vector
index(2,4,2) ## this is the position in the original vector
index(3,4,2) ## this is the position in the original vector
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.