2
$\begingroup$

I have the following matrix with Dimensions[A] = {5}. What is the easiest way to convert it to $8 \times 8$ matrix?

A = {{{1, 1, 1, 1, 1, 1, 1, 1}}, {{1, 3, 2, 8, 6, 5, 9, 2}}, 
     {{1, 8, 9, 8, 2, 7, 7, 5}, {7, 9, 6, 1, 8, 7, 6, 3}}, 
     {{2, 9, 5, 5, 1, 7, 9, 7}, {9, 4, 8, 3, 5, 6, 2, 6}, 
     {1, 2, 5, 3, 3, 8, 5, 2}, {4, 4, 2, 3, 6, 8, 7, 7}}, 
     {{6, 5, 9, 3, 3, 5, 8, 2}, {4, 1, 4, 7, 4, 4, 8, 2}, 
     {4, 8, 8, 6, 6, 4, 6, 3}, {8, 4, 1, 6, 6, 3, 2, 1}, 
     {4, 9, 6, 3, 6, 9, 5, 6}, {9, 2, 5, 8, 6, 3, 5, 6}, 
     {4, 2, 2, 2, 2, 5, 8, 6}, {6, 2, 2, 6, 9, 9, 5, 2}, 
     {6, 4, 7, 6, 8, 9, 1, 8}}};
$\endgroup$
5
  • 1
    $\begingroup$ Let me first make sure I understand if this is what you want. ArrayReshape[Flatten[A], {8, 8}]; Dimensions[%] gives {8,8} but your original matrix has 136 entries, not 64? $\endgroup$
    – Nasser
    Jun 8 at 14:06
  • $\begingroup$ @Nasser Yes that's what I was looking for, thanks. I wish you left the answer I would have accepted it. $\endgroup$
    – Morcus
    Jun 8 at 14:06
  • $\begingroup$ OK, it put the answer back. I just was not sure if this is what you wanted as the number of entries is different, that is why. $\endgroup$
    – Nasser
    Jun 8 at 14:07
  • 1
    $\begingroup$ Dimensions /@ A evaluates to {{1, 8}, {1, 8}, {2, 8}, {4, 8}, {9, 8}} and Length@Flatten@A evaluates to 136 Which 64 elements are supposed to be in the final 8x8 matrix? $\endgroup$
    – Bob Hanlon
    Jun 8 at 14:11
  • 1
    $\begingroup$ Very strong recommendation: Post the MINIMAL data/code that illustrates your problem. Do we really need to see every number to 12 digits?? The simpler version will help you see the answer yourself, oftentimes. $\endgroup$ Jun 8 at 14:44

3 Answers 3

4
$\begingroup$

If I understand you right

ArrayReshape[Flatten[A], {8, 8}];
Dimensions[%]

Mathematica graphics

btw, the title of you question says

How to remove extra parentheses in a matrix

If this is what you really wanted, then the command is

Flatten[A, 1]
Dimensions[%]

Mathematica graphics

So may be you wanted the first 8 rows of the above only and discard the rest of the rows? In this case, this also works

Flatten[A,1];
%[[1;;8,All]];
Dimensions[%]

Mathematica graphics

$\endgroup$
2
$\begingroup$

Your list seems to have the structure:

{Union@Flatten[A], Length[B=Flatten[A,1]], Length/@#&/@A}
(* {{1, 2, 3, 4, 5, 6, 7, 8, 9}, 17,
 {{8}, {8}, {8, 8}, {8, 8, 8, 8}, {8, 8, 8, 8, 8, 8, 8, 8, 8}}} *)

At the lowest level you have $17$ Lists of length $8$ which are of integers from $1$ to $9$. These are collected in matrix $B$. Some $8$ rows of this matrix can be collected to form an $8 \times 8$ matrix. The most direct way is to do this is Take[B,8] for the first $8$ rows but any other $8$ rows from B can be used. For example Take[B,-8] gives the last $8$ rows. It depends on what you want to extract from A.

$\endgroup$
0
$\begingroup$

If you want the first eight 8-vectors (as in @Nasser's answer):

B = Take[
  Level[A, {-2}], (* all row vectors *)
  8]
B // Dimensions
(*
{{...}}
{8, 8}
*)
$\endgroup$

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