2
$\begingroup$

I have a vector composed of 10 elements and I want to "split" it into two vectors composed of five elements:

q={1,2,3,4,5,6,7,8,9,10};Partition[q,5]

but the output is a matrix...

{{1,2,3,4,5},{6,7,8,9,10}}

How can I obtain 2 separated vectors rather than a matrix?

$\endgroup$
  • 2
    $\begingroup$ What are 'two separated vectors'? Sequence @@ Partition[q,5]? It will still have Sequence head but that is the closest concept I suppose. $\endgroup$ – Kuba Jun 22 '17 at 15:10
  • $\begingroup$ @tomd: Huh? Doesn't your answer give precisely what the poser's (unsatisfactory) answer gives? $\endgroup$ – David G. Stork Jun 22 '17 at 16:05
  • $\begingroup$ Don't think of it as a matrix, think of it as a list of vectors. You can do [[1]] or //First or any other list operation to extract the first and second vectors. $\endgroup$ – 1110101001 Jun 22 '17 at 19:45
5
$\begingroup$

The output of a line of Mathematica code is a single element, so

Partition[q,5]

is all you can get. If you want to access the parts individually:

Partition[q,5][[1]] 

and

Partition[q,5][[2]] 

What exactly do you want this for? (That will help us help you.)

$\endgroup$
6
$\begingroup$

How about:

q = {1,2,3,4,5,6,7,8,9,10};
{part1, part2} = Partition[q,5]

Now you have two separate lists (part1 and part2), each containing half of the partitioned q.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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