12

If I have an array A, for which Dimensions[A] is {9, 9, 1, 1, 3, 4}, how can I convert A' into an array with dimensions {9, 9, 3, 4}?

I think there is a built-in function for this, but I can't remember its name.

rm -rf
  • 88,781
  • 21
  • 293
  • 472
LCFactorization
  • 3,047
  • 24
  • 37
  • What is meant by conversion of dimension ? Repositioning element or just removing 1 from list ? – Pankaj Sejwal Nov 23 '13 at 09:13
  • It is just the same array logically; however, mathematica's Dimensions[] function will identify them as different arrays with different dimensions. – LCFactorization Nov 23 '13 at 09:17
  • E.g, If I have {{a,b},{c,d}}, it is {2,2} in sizes; sometimes it can also be {2,2,1,1,1}; How to make sure mathematica's Dimensions only obtain the simplest result {2,2}? – LCFactorization Nov 23 '13 at 09:18
  • Flatten does work, but it is complicate this way; there should be a simpler way with only one simple Function but I forget the name. – LCFactorization Nov 23 '13 at 09:27

3 Answers3

13

You could get the array's dimensions, remove the 1's and use that in ArrayReshape:

array = Array[f, {9, 9, 1, 1, 3, 4}];
new = ArrayReshape[array, Dimensions[array] ~DeleteCases~ 1];
Dimensions[new]
(*  {9, 9, 3, 4} *)

If efficiency is not important you could use a replacement rule:

new = array //. {x_} :> x
Dimensions[new]
(*  {9, 9, 3, 4} *)
Simon Woods
  • 84,945
  • 8
  • 175
  • 324
3
array = Array[f, {9, 9, 1, 1, 3, 4}];
Dimensions[array]

{9,9,1,1,3,4}

newArray=Flatten[array , {{1}, {2}, {5}, {6, 3, 4}}];  
 Dimensions[newArray]

{9,9,3,4}

andre314
  • 18,474
  • 1
  • 36
  • 69
  • Thank you. The desired solution should be that, the user don't have to use the original array's dimension information. If these are available, I would prefer to use ArrayReshape[] – LCFactorization Nov 23 '13 at 09:36
3

Another way to use Flatten is to collect the trivial (single-element) levels with the first nontrivial level:

flattenTrivialLevels[array_] := 
 Flatten[array, 
   Flatten[{Position[#, _?(# > 1 &), 1, 1], Position[#, 1]} &@ Dimensions[array]]]

OP's test case (compared with Simon Woods' elementary replacement method):

array = RandomInteger[10, {9, 9, 1, 1, 3, 4}];
flat = flattenTrivialLevels[array];
new = array //. {x_} :> x;
Dimensions[flat]
flat === new

(* {9, 9, 3, 4} *)
(* True *)

The Flatten command being used is Flatten[array, {1, 3, 4}].

An arbitrary variation:

array = RandomInteger[10, {1, 9, 1, 9, 1, 1, 3, 1, 1, 1, 4}];
flat = flattenTrivialLevels[array];
new = array //. {x_} :> x;
Dimensions[flat]
flat === new

(* {9, 9, 3, 4} *)
(* True *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747