Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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??

share|improve this question

3 Answers 3

(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.

share|improve this answer
    
the data are stored in a *.dat file and they supposed to be numbers and not string. –  Amin May 29 '13 at 16:03
    
@Amin Just IntegerDigits/@Import@"E:\\Users\\Quaternions\\Documents\\test.dat" –  HyperGroups May 29 '13 at 16:06
    
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. –  Amin May 29 '13 at 17:07
    
I used IntegerDigits/@Import@"E:\\data\\data.txt","List] and it worked but it gave me some errors –  Amin May 29 '13 at 17:42
    
@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. –  geordie May 30 '13 at 0:48
MatrixForm[
     Read /@ StringToStream /@ Characters[#] & /@ {"1234" , "5678"}]

enter image description here

perhaps a bit cleaner..

 IntegerDigits /@ Read /@ StringToStream /@ {"1234", "5678"}
share|improve this answer

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?

share|improve this answer
    
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. –  Amin May 29 '13 at 19:09
    
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. :) –  Oleksandr R. May 29 '13 at 19:17
    
@OleksandrR. I hope to get the hang of this. –  Hans May 29 '13 at 19:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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