2
$\begingroup$

I have a list with dimensions {1000, 1000, 1} and I would like it to have dimensions {1000, 1000}. But Flatten[list,1] makes it {1000000, 1} and I've also tried Flatten[list,-1] with no success. Can I use flatten to do this or will I need a different command?

$\endgroup$
  • 4
    $\begingroup$ Maybe Flatten /@ list? $\endgroup$ – anderstood Feb 19 '18 at 16:17
  • $\begingroup$ @anderstood Perfect, that works, thanks! $\endgroup$ – user13948 Feb 19 '18 at 16:23
  • 3
    $\begingroup$ Also list[[All, All, 1]] will work. $\endgroup$ – Carl Woll Feb 19 '18 at 16:34
  • 4
    $\begingroup$ Flatten[list, {1, 3}] also works, but ArrayReshape[list, Most@Dimensions@list] might be fastest. $\endgroup$ – Michael E2 Feb 19 '18 at 17:12
0
$\begingroup$

Given a list of similar shape to the one in the question:

$a = Array[a[##] &, {3, 3, 1}];
$a // MatrixForm

unflattened array

... we can tell Flatten to leave level one untouched but flatten levels two and three together:

Flatten[$a, {{1}, {2, 3}}] // MatrixForm

partially flattened array

This syntax is the subject of the question Flatten command: matrix as second argument.

|improve this answer|||||
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.