2
$\begingroup$

Suppose I have a data file containing one column of 1000 rows having x, y, z and ϕ values. I need to separate them into four distinct columns by selecting every alternative value.

For example, if I have a column such as {a,b,c,d,e,f,g,h,i,j}, then I need to select {a,c,e,g,i} for one column and {b,d,f,h,j} for another column.

z = RandomReal[{0, 1}, 1000]

How do I do that?

$\endgroup$
4
  • $\begingroup$ If crange = CharacterRange["a", "z"] then consider: (t0 = Partition[crange, 4]) // Column or t1 = Transpose@Partition[crange, 4]. I am not sure which one you want. Do you want to rewrite this file back as a four column file? If so, can you include a smaller sample file with perhaps 100 entries. $\endgroup$
    – Syed
    Jun 24, 2022 at 18:36
  • $\begingroup$ Yes, actually, the data file contains a single column having x,y,z, and phi values. I need to separate them into four distinct columns $\endgroup$
    – user444
    Jun 24, 2022 at 18:44
  • 1
    $\begingroup$ @user84456 From your last description, then would a simple Partition[z, 4] work? $\endgroup$
    – MarcoB
    Jun 24, 2022 at 19:04
  • $\begingroup$ If Partition produces a 'ragged' array, perhaps you require Flatten[Partition[{a,b,c,d,e,f,g,h,i,j,k},UpTo[2]],{{2}}] (which in this case gives {{a, c, e, g, i, k}, {b, d, f, h, j}}) $\endgroup$
    – user1066
    Jun 24, 2022 at 22:28

2 Answers 2

3
$\begingroup$

If I understand what you seek, then with z = Range[32] for clarity, maybe:

First@ Multicolumn[z, {4, Automatic}]

(* Out: 
{{1, 5, 9, 13, 17, 21, 25, 29},
 {2, 6, 10, 14, 18, 22, 26, 30}, 
 {3, 7, 11, 15, 19, 23, 27, 31}, 
 {4, 8, 12, 16, 20, 24, 28, 32}}
*)
$\endgroup$
2
$\begingroup$

First save a list of random numbers to a file to create a mock experiment:

alist = RandomReal[{0, 1}, 100]
Export["C:/alist.txt", alist, "List"]

Then read it back like so:

t1=ReadList["C:/alist.txt", {Number, Number, Number, Number}]

Export it back like this:

Export["C:/alist2.txt", t1, "Table"]

Try Table and List as formats to see the difference.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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