Message Boards Message Boards

1
|
5718 Views
|
6 Replies
|
11 Total Likes
View groups...
Share
Share this post:
GROUPS:

How to avoid the nested list in the command table?

Posted 9 years ago

Hi guys, using a code like this:

Table[{{i, j, k, l}}, {i,1,2}, {j,1,2}, {k,1,2}, {l,1,2}] // MatrixForm

I obtain a nested list:

enter image description here

Instead of that, I'd like to obtain just a simple array composed of 16 vectors 1x4 like this one:

enter image description here

I need that because i have to do those calculations:

vectors = 
  Table[KroneckerProduct[misure[[i]], misure[[j]], misure[[k]], 
    misure[[l]]], {i, 1, 2}, {j, 1, 2}, {k, 1, 2}, {l, 1, 2}];

aspectedvalue = 
  Table[FullSimplify[
    Abs[KroneckerProduct[misure[[i]], misure[[j]], misure[[k]], 
        misure[[l]]].ConjugateTranspose[\[Psi]]]^2, 
    Assumptions -> {\[Phi] \[Element] Reals}], {i, 1, 2}, {j, 1, 
    2}, {k, 1, 2}, {l, 1, 2}];

and I want to obtain a simple output in both.

In the first one, I'd like to obtain a 16x(1x16) array of vectors (misure[[i]] are 1x2 vectors, and the kronecker product of 4 of them gives a 1x16 vector); in the latter I'd like to obtain a 16x1 vector (because those are just scalars.). If there isn't a clever way to do this with table, I'll use for cycles...

I hope I made myself clear, thank you

6 Replies

Thank you all guys, at this moment I have done in this way:

Table[KroneckerProduct[misure[[i]], misure[[j]], misure[[k]], 
   misure[[l]]], {i, 1, 2}, {j, 1, 2}, {k, 1, 2}, {l, 1, 2}];
Flatten[%];
linearvectors = ArrayReshape[%, {16, 1, 16}];

Is there any way to put all those commands in one line? (I'm sorry for those idiot questions, this is my second day using mathematica and I had no time to read a manual :( )

linearvectors = KroneckerProduct @@@ (misure[[#]] & /@ Tuples[{1, 2}, 4])
POSTED BY: Kuba Podkalicki

just to show different approach:

table // Cases[#, _?VectorQ, {-2}] &
POSTED BY: Kuba Podkalicki
Posted 9 years ago

You can "unnest" it using Flatten:

Table[{{i, j, k, l}}, {i, 1, 2}, {j, 1, 2}, {k, 1, 2}, {l, 1, 2}] ~Flatten~ 4 // MatrixForm

Output

POSTED BY: Karsten 7.

This is a job for ArrayReshape

r = Table[{i, j, k, L0},
  {i, 1, 2},
  {j, 1, 2},
  {k, 1, 2},
  {L0, 1, 2}]
  (ArrayReshape[r, {16, 4}]) // MatrixForm

enter image description here

ps. in this specific case, as was mentioned above, Tuples[{1, 2}, 4] will do the same and much easier.

POSTED BY: Nasser M. Abbasi

How about using Tuples:

Tuples[{1, 2}, 4] // MatrixForm
POSTED BY: Kay Herbert
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract