# Import matrices side-by-side separated by TAB

The problem is 0(TAB)0 interpreted falsely as a dark red square although it should be a dark red square surrounded by zeroes/ones. Import has different data-formats such as Data and Matrix but I don't know which one this is: ASCII? So how can I import the below type data as a text-file to Mathematica and get the goal-style matrixplot? Is there some format command to specify the structure of the data for importing?

Goal

Failure

Data

0,0,1,1,1,1,0   0,1,1,1,1,1,0
0,0,1,1,1,1,0   0,0,1,1,1,1,0
0,0,0,0,1,1,0   0,0,0,1,1,1,0
0,0,0,0,1,1,0   0,0,0,0,1,1,0
0,0,0,0,0,1,0   0,0,0,0,0,0,0
0,0,0,0,0,0,0   0,0,0,0,0,0,0
1,1,1,1,1,1,0   1,1,1,1,1,1,0


-
Look at FullForm of your imported data and find out what the erroneous character looks like, then remove it. If the data isn't in the right dimensions after removal, use Partition or Transpose to get it reformatted. –  bill s May 6 '13 at 11:44
@bills it is the "0\t0" or "0\t1", it should be interpreted as zero TAB zero or zero TAB one -- but it interprets it as a special something? –  hhh May 6 '13 at 11:47
So the first step is to DeleteCases[data,"0\t0"]. This will give you the right data but in one big matrix. Then you can use Part to separate out the first half from the second half. –  bill s May 6 '13 at 11:49
I think it should be possible to tweak the import so that it will import the data as you expect. Something like Import["~/R1", "Table", "FieldSeparators" -> {" ", ",", "\t"}] should do the job... –  Albert Retey May 6 '13 at 13:34
@AlbertRetey look I want to have that kind of separator -- if I do this I lose the separation between the matrices. I want to compare the matrices side-by-side with some separator. –  hhh May 6 '13 at 19:02

A way to do this is to use rules. You have four things that can happen, so you can write four rules to substitute.

data//. {"0\t0" -> {0, 0}, "1\t0" -> {1, 0}, "0\t1" -> {0, 1}, "1\t1" -> {1, 1}}


Then you need to reshape the data. Flatten the output of the above and then partition it into the matrices of the correct size:

data2 = Partition[Flatten[data], 14]


then take the two portions you desire data2[[All,1;;7]] and data2[[8;;8;;14]]

-
Look I can specify the input format of the matrix myself. Do you mean it is better to create this kind of rule than use some ready import format parameter? My goal is to compare many matrices side-by-side and I decided to use comma to separate matrix entries and TABS to separate different matrices -- there must be some way to create this kind of comparison -- two TABs embedded with extra zeroes? No, looks probably ugly. –  hhh May 6 '13 at 19:02
Once you have the matrices defined, you can display them any way you like, side by side, top to bottom, superimposed. If you get to choose the data structure for the import, then you're going about this the wrong way. Make the import into Mathematica easy and then use the powerful display and graphics representations in Mathematica to do the display. You can easily create the display you show at the top once you have the data as two lists. Row[{ArrayPlot[m1],ArrayPlot[m2]}] will display them side by side, for instance. –  bill s May 6 '13 at 19:49
Now you don't understand, I get matrices in ASCII and I can cut/paste/use-any-unix-command to preprocess them to make them easy for Mathematica. How can I reformat many ASCII matrices so easy to plot with Mathematica? Keep everything in separate files or put them to one file like the question with TAB separation? –  hhh May 6 '13 at 23:13
You can read them all in at once, and do the formatting in Mathematica, or you can place them in separate files (you do the formatting in unix). Personally, I would be tempted to put them all in separate files and then to read them in using Table[Import[ ... ]]. –  bill s May 7 '13 at 3:52

There are of course many possibilities to achieve what you want. Here is one which imports and then rearranges, of course that will only work if you know how many columns per matrix there are:

data = Insert[#, "|", 8] & /@
Import[ToFileName[{$HomeDirectory}, "R1"], "Table", "FieldSeparators" -> {" ", ",", "\t"}]  Here is another method which imports the file content as string, manipulates it and then imports the result from the string with ImportString, which also seems to do what you want, is very similar to bills answer and doesn't need any knowledge about the number of columns: data = ImportString[ StringReplace[ Import[ToFileName[{$HomeDirectory}, "R1"],
"Text"], (" " | "\t") .. :> ",\"|\","],
"CSV"]


data then in both cases is a matrix which contains both matrices and divides them with the string "|" and will plot as you indicate with MatrixPlot. Of course you can use something else as a divider just as well, the plot will look the same as long as the dividers aren't numeric values. If your data is huge (which probably is not the case if stored in a text file), you might want to use an integer value as divider so the array can be packed. Either way, as long as your dividers are unique in the data, you can then extract each matrix with something like:

data[[All, 1 ;; Position[data[[1]], "|"][[1, 1]] - 1]]
data[[All, Position[data[[1]], "|"][[1, 1]] + 1 ;; -1]]

-