[R] About 5.1 Arrays
Joshua Wiley
jwiley.psych at gmail.com
Mon Nov 8 01:33:50 CET 2010
On Sat, Nov 6, 2010 at 7:38 AM, Stephen Liu <satimis at yahoo.com> wrote:
> Hi Joshua,
>
> Thanks for your advice.
>
> 1)
> Re your advice:-[quote]
>> a3d
> , , 1 <--- this is the first position of the third dimension ***THIS IS THE THIRD DIMENSION***
>
> [,1] [,2] [,3] [,4] <--- positions 1, 2, 3, 4 of the second dimension
> [1,] 1 4 7 10
> [2,] 2 5 8 11
> [3,] 3 6 9 12
> ^ the first dimension
>
> , , 2 <--- the second position of the third dimension ***THIS IS THE THIRD DIMENSION***
> ...
> [/quote]
>
> Where is the third dimension?
I pointed to the third dimension above. You can think of a 3d array
kind of like a journal (I don't know if this is helpful, but I kind of
like the analogy so...). Each page holds a two dimensional table, so
I could tell you to look at row 4, column 3 on page 16. Nevertheless,
at any given point, it is just a flat page.
>
> 2)
> Re your advice:-[quote]
> so you can think that in the original vector "a":
> 1 maps to a[1, 1, 1] in the 3d array
> 2 maps to a[2, 1, 1].
> 3 maps to a[3, 1, 1]
> 4 maps to a[1, 2, 1]
> 12 maps to a[3, 4, 1]
> 20 maps to a[2, 3, 2]
> 24 maps to a[3, 4, 2]
> [/quote]
>
> My finding;
>
> # 1 maps to a[1, 1, 1] in the 3d array
>> a3d <- array(a, dim = c(1, 1, 1))
>> a3d
> , , 1
>
> [,1]
> [1,] 1
>
> Correct
>
> # 2 maps to a[2, 1, 1].
>> a3d <- array(a, dim = c(2, 1, 1))
>> a3d
> , , 1
>
> [,1]
> [1,] 1
> [2,] 2
>
> Correct
>
> # 3 maps to a[3, 1, 1]
>> a3d <- array(a, dim = c(3, 1, 1))
>> a3d
> , , 1
>
> [,1]
> [1,] 1
> [2,] 2
> [3,] 3
>
> Correct
>
> # 4 maps to a[1, 2, 1]
>> a3d <- array(a, dim = c(1, 2, 1))
>> a3d
> , , 1
>
> [,1] [,2]
> [1,] 1 2
>
> Incorrect. It is "2"
No, it is correct. You cannot assume that you may use the same
indices to access an array when you have created it with different
dimensions. Consider:
> array(1:24, dim = c(3, 4, 2))[1, 2, 1]
[1] 4
> array(1:24, dim = c(1, 2, 1))[1, 2, 1]
[1] 2
>
>
> # 12 maps to a[3, 4, 1]
>> a3d <- array(a, dim = c(3, 4, 1))
>> a3d
> , , 1
>
> [,1] [,2] [,3] [,4]
> [1,] 1 4 7 10
> [2,] 2 5 8 11
> [3,] 3 6 9 12
>
> Correct
>
> # 20 maps to a[2, 3, 2]
>> a3d <- array(a, dim = c(2, 3, 2))
>> a3d
> , , 1
>
> [,1] [,2] [,3]
> [1,] 1 3 5
> [2,] 2 4 6
>
> , , 2
>
> [,1] [,2] [,3]
> [1,] 7 9 11
> [2,] 8 10 12
>
> Incorrect. It is "12"
See my above comment about not expecting things in the same location
when you change the space they live in.
Sorry this was so slow in coming, I missed the email somehow.
Cheers,
Josh
[snip]
More information about the R-help
mailing list