Remi.b
Remi.b

Reputation: 18219

How to reformat an array of arrays (or tuples) into an array of higher dimension

Consider these two one-dimensional array of tuples and array

a = [ (x,x,x)  for x=1:5 ]
b = [ [x,x,x]  for x=1:5 ]
c = [ {x,x,x}  for x=1:5 ]

What is the easiest way to format these arrays a b or c into the array d?

d = reshape(repeat([x for x=1:5],outer=[3]),5,3)
5x3 Array{Int64,2}:
 1  1  1
 2  2  2
 3  3  3
 4  4  4
 5  5  5

Upvotes: 2

Views: 38

Answers (1)

IainDunning
IainDunning

Reputation: 11654

Due to the auto-concatenation that happens with vectors, the best (shortest) way for b and c is

hcat(b...)'

For a, because they are tuples, I'd do something like

hcat(map(t->[t...],a)...)'

where I'm converting the tuples to arrays, then concatenating them.

Upvotes: 1

Related Questions