1
$\begingroup$

I have a data set in form of:(this is just an example)

1324501020
3241030205
4332020134

the data are stored in a text file (e.g. data.txt) but I need to convert them into a matrix format such that each number be place in a cell like this:

1 3 2 4 5 0 1 0 2 0
3 2 4 1 0 3 0 2 0 5
4 3 3 2 0 2 0 1 3 4

or in terms of List in Mathematica, I need to have

{{1,3,2,4,5,0,1,0,2,0},{3,2,4,1,0,3,0,2,0,5},{4,3,3,2,0,2,0,1,3,4}}

in other words, the final data set supposed to be a matrix of numbers. Any idea??

$\endgroup$
0

3 Answers 3

1
$\begingroup$

(IntegerDigits@ImportString["1324501020 3241030205 4332020134"]) // Flatten[#, 1] & list data no need spaces?

If you wanna string still add them Riffle[#, " "] & /@ Characters /@ StringSplit["1324501020 3241030205 4332020134"]

If data in txt file, import as list first.

$\endgroup$
5
  • $\begingroup$ the data are stored in a *.dat file and they supposed to be numbers and not string. $\endgroup$
    – Amin
    Commented May 29, 2013 at 16:03
  • $\begingroup$ @Amin Just IntegerDigits/@Import@"E:\\Users\\Quaternions\\Documents\\test.dat" $\endgroup$ Commented May 29, 2013 at 16:06
  • $\begingroup$ I couldn't use those syntax. I've imported the file into Mathematica but the operation by IntegerDigits is change them into numbers from string. I need to have a numerical matrix at the end. May be I'm missing some thing here. $\endgroup$
    – Amin
    Commented May 29, 2013 at 17:07
  • $\begingroup$ I used IntegerDigits/@Import@"E:\\data\\data.txt","List] and it worked but it gave me some errors $\endgroup$
    – Amin
    Commented May 29, 2013 at 17:42
  • $\begingroup$ @Amin It needs to be IntegerDigits/@Import["E:\\data\\data.txt","List"]. The shorthand @ can only be used when a function has one argument and no options. Actually, you only need IntegerDigits@ (rather than .../@) since IntegerDigits is listable. $\endgroup$
    – geordie
    Commented May 30, 2013 at 0:48
1
$\begingroup$

Try:

TableForm[IntegerDigits[{1324501020, 3241030205, 4332020134}]]
(* ->
   1    3   2   4   5   0   1   0   2   0
   3    2   4   1   0   3   0   2   0   5
   4    3   3   2   0   2   0   1   3   4
*)

This will make the list look like a matrix table form. HyperGroups is correct in trying IntegerDigits. What kinds of operations will you be performing on the results or is this purely formatting for display purposes?

$\endgroup$
3
  • $\begingroup$ Those are data from a testing company and numbers are responses to items provided by test takers. They have some kind of scanners for reading scantrons and store responses after each other without space. I want to analyze responses so I need to have them separated in which responses to each item be like a variable or in terms of matrix data, be a column. $\endgroup$
    – Amin
    Commented May 29, 2013 at 19:09
  • $\begingroup$ Welcome to the site, and thanks for your answer! I made a few edits to your post in order to conform to site norms. I'm sure you'll quickly get the hang of it. :) $\endgroup$ Commented May 29, 2013 at 19:17
  • $\begingroup$ @OleksandrR. I hope to get the hang of this. $\endgroup$
    – Hans
    Commented May 29, 2013 at 19:35
0
$\begingroup$
MatrixForm[
     Read /@ StringToStream /@ Characters[#] & /@ {"1234" , "5678"}]

enter image description here

perhaps a bit cleaner..

 IntegerDigits /@ Read /@ StringToStream /@ {"1234", "5678"}
$\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.