3
$\begingroup$

I have data in a rectangular structure – something like matrix={{a,b,c},{d,e,f},{g,h,i}}. The problem is that some of the columns consist of uncompressed data and some not. Now I'm looking for an elegant way to uncompress the whole matrix. Unfortunately Uncompress[matrix] doesn't work.

$\endgroup$
5
  • $\begingroup$ Do you know which columns are compressed? $\endgroup$ Sep 4, 2012 at 13:46
  • $\begingroup$ Hi! you asked seven questions, but accepted answers for only two of them. Some of the answers you received in the (still) unaccepted questions are very good. I think you should go thru them and accept the better ones. $\endgroup$ Sep 4, 2012 at 14:01
  • $\begingroup$ yes I do know, but it can vary from case to case. So a general solution would be more convenient;-) $\endgroup$
    – RMMA
    Sep 4, 2012 at 14:02
  • $\begingroup$ @Verde you are right. Got really perfect answers indeed. Will acept them! $\endgroup$
    – RMMA
    Sep 4, 2012 at 14:07
  • $\begingroup$ Also, are you aware of vote casting? It is very important to enhance the visibility of good questions and answers! $\endgroup$ Sep 4, 2012 at 14:47

2 Answers 2

2
$\begingroup$

Create a matrix, and compress a column

a = RandomInteger[10, {3, 3}]
b = Transpose@{a[[1]], Compress /@ a[[2]], a[[3]]}

Uncompress it:

Quiet@Map[Check[Uncompress[#], #] &, a, {2}]

Edit

Instead of Quiet[] you could use Off[Uncompress::string] so other error messages are not hindered

$\endgroup$
5
  • $\begingroup$ Better to use ReplaceAll with a pattern str_String /; StringMatchQ[str, StartOfString ~~ DigitCharacter ~~ ":" ~~ __] :> Check[Uncompress[str], str, Uncompress::corrupt]? $\endgroup$ Sep 4, 2012 at 14:02
  • $\begingroup$ it seems as if it doesnt work in my case. Maybe because the compressed part can be again a list. In that case I would like to write only the 7th element into the uncompressed matrix. $\endgroup$
    – RMMA
    Sep 4, 2012 at 14:06
  • 3
    $\begingroup$ @rainer Why don't you then tell us what your "case" is? Your question is vague and this is the most that can be said without divining your thoughts $\endgroup$
    – rm -rf
    Sep 4, 2012 at 14:08
  • $\begingroup$ @OleksandrR. I am not aware that the : is documented. If it is, you are right $\endgroup$ Sep 4, 2012 at 14:14
  • $\begingroup$ ok, now it works. Thx! $\endgroup$
    – RMMA
    Sep 4, 2012 at 14:42
4
$\begingroup$

Here is a way to only uncompress elements that have been compressed. Note that using Mathematica's Compress returns a string starting with <number>:, so we need to Uncompress only those elements that are strings and start with that.

Clear@uncompress
uncompress[s_String] /; StringMatchQ[s, DigitCharacter ~~ ":" ~~ ___] := Uncompress@s
uncompress[x_] := x
$\endgroup$
10
  • $\begingroup$ The characters "1:e" only appear at the beginning for version 1 compression. Version 2 seems to have "2:G". I would tend to consider only the version number and colon to be part of the header proper since I don't know where the "e" or "G" come from... $\endgroup$ Sep 4, 2012 at 14:08
  • $\begingroup$ Is the : documented anywhere? $\endgroup$ Sep 4, 2012 at 14:11
  • $\begingroup$ @OleksandrR. There is a version 2 of Compress? I've only ever seen 1:e, but I've changed it to DigitCharacter ~~ ":" since you say you've seen 2:G $\endgroup$
    – rm -rf
    Sep 4, 2012 at 14:12
  • $\begingroup$ @Verde Trying to find it, but not sure if it is $\endgroup$
    – rm -rf
    Sep 4, 2012 at 14:14
  • 1
    $\begingroup$ @Verde the fact that Uncompress emits a message when passed something other than a string representing compressed data isn't documented either, but your answer relies on it. By requiring the parameter to be a string of a certain form rather than passing everything indiscriminately, you reduce the chances of encountering side effects or passing some garbage that Uncompress may choke on. Of course the optimum solution is avoid creating data structures that require heuristics to reinterpret in the first place... $\endgroup$ Sep 4, 2012 at 15:15

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.