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.
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.
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} *)
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}
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 *)