Skip to content

problem creating an array

3 messages · Curtis Burkhalter, Rui Barradas, Bert Gunter

#
Hello,

You don't need the loop, it can be done with one much simpler instruction.
Since the number of rows is not important, the example uses a 10x603 matrix.

x <- matrix(seq_len(10*603), ncol = 603)
y <- array(x, dim = c(10, 3, 201))  # that's it

# See the first two
y[, , 1:2]

Hope this helps,

Rui Barradas
Em 13-09-2012 23:04, Curtis Burkhalter escreveu:
#
Well, Rui -- I'll bet that that gobSmacked him (and probably others)!
So here's just a tiny extra effort at enlightenment.

The keys are understanding that:

1. Matrices are (formally) just 2-d arrays.
2. All arrays are just vectors, indexed in "column major" order --
i.e. rows vary the fastest, then columns, then layers, then ...  And
this is documented (tersely) in ?array as

"The values in data are taken to be those in the array with the
leftmost subscript moving fastest."

If you parse this out carefully, you'll see why Rui's magic works.

-- Bert
On Thu, Sep 13, 2012 at 7:00 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote: