1
|
6242 Views
|
6 Replies
|
11 Total Likes
View groups...
Share
GROUPS:

# How to avoid the nested list in the command table?

Posted 10 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: Instead of that, I'd like to obtain just a simple array composed of 16 vectors 1x4 like this one: 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
Sort By:
Posted 10 years ago
 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 :( )
Posted 10 years ago
 linearvectors = KroneckerProduct @@@ (misure[[#]] & /@ Tuples[{1, 2}, 4]) 
Posted 10 years ago
 just to show different approach: table // Cases[#, _?VectorQ, {-2}] & 
Posted 10 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 
Posted 10 years ago
 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 ps. in this specific case, as was mentioned above, Tuples[{1, 2}, 4] will do the same and much easier.
Posted 10 years ago
 How about using Tuples: Tuples[{1, 2}, 4] // MatrixForm