5
$\begingroup$

I loaded a list with 400,000 values from a file. The list was loaded correctly as I can get it's length (by Length[]) and I can also plot it.

Why can't I get the Max or Min of this list though? When I try to evaluate the cell, I simply get "Max[all the values of my list]" as output.

My file contains a list of floats, each on its own line, separated by a newline.

I load them like this:

list = ReadList["myfile.txt"]

Mathematica converts some of the floats into a weird "integer + float e" format, for example "9 + 1.4443 e". Thus, not all values are numerical but I still need to take them into account.

$\endgroup$
9
  • 2
    $\begingroup$ Could you provide a small sample of your list (eg. myList[[ 1 ;; 10 ]] or by using Short) ? $\endgroup$
    – gwr
    Jan 31, 2016 at 15:39
  • $\begingroup$ @gwr {0.26304, -0.0398959, -0.330003, -0.565977, -0.734065, -0.813924, \ -0.796839, -0.702942, -0.544771, -0.345005} $\endgroup$ Jan 31, 2016 at 16:16
  • $\begingroup$ I copied and pasted your list but I notice that there is a '\' before the 7th element. Could this be what is wrong? $\endgroup$
    – Conor
    Jan 31, 2016 at 16:22
  • 1
    $\begingroup$ Possible duplicate of Removing not-a-number entries from an array of numbers $\endgroup$
    – garej
    Jan 31, 2016 at 16:33
  • 1
    $\begingroup$ @JohnConorCosnett No, the backslash must have been a copy+paste bug. $\endgroup$ Jan 31, 2016 at 17:00

2 Answers 2

11
$\begingroup$

Proper Input of file and checking the list

Make sure that the variable list does contain your file inputs and make sure that Mathematica correctly understands your numbers. For this you might try:

list = SemanticImport[ "myfile.txt", "Number", "List" ]

or using ReadList you might try:

list = ReadList[ "myfile.txt", "Number" ] 

Then check whether there still is anything besides numeric quantities:

list // Map[ NumericQ] // Apply[And]

If everything is ok, then you should see True as a result.

To further ponder the Plus problem you should check for what exactly happens by looking at the result of:

Select[ list, Head[#] == Plus & ][[1]] // FullForm

As we have discussed in the comments here, you probably have read in some number like "10e+3" which causes problems if not addressed by giving the type as in above uses of SemanticImport or ReadList.

Finding the Max and Min anyway

If your result was False you may get some result anyway but simply just selecting the numeric elements:

list // RightComposition[
    Select[NumericQ], 
    Through[{Max, Min}[#]] &
  ]

Fixing the Plus expressions returned for floats

To address the problem of Plus given for exponential numbers you may also try:

list = list /. Plus[ exp_ , Times[ coef_, e ] ] :> coef \[Times] 10.^exp

That should take care of the false representation using ReadList without giving the type "Number".

$\endgroup$
3
  • $\begingroup$ Returns false since Mathematica transforms my float numbers into something like "integer + float", thus making the list contain pluses. Your function seems awesome, but unfortunately the results will be a little off since the "transformed" floats won't be taking into account. Thanks for your help though! $\endgroup$ Jan 31, 2016 at 17:05
  • $\begingroup$ Thanks, this solved everything! $\endgroup$ Jan 31, 2016 at 18:18
  • 1
    $\begingroup$ Fyi, SemanticImport is not currently reliable for many variable data types, eg dates and times. $\endgroup$ Feb 1, 2016 at 5:00
3
$\begingroup$

I am only a beginner at Mathematica but I notice that Max is left unevaluated if given non numerical data types:

enter image description here

I recommend sampling your data:

RandomSample[lista,10}

and applying the Head[] function to a few elements which returns the data type

$\endgroup$
2
  • $\begingroup$ My list only contains numerical types, though? $\endgroup$ Jan 31, 2016 at 16:18
  • 3
    $\begingroup$ @GandalfSchmidt : Your file contains numerical types. You haven't provided any examples of what your list contains that isn't a simple number. $\endgroup$ Jan 31, 2016 at 18:22

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.