# There is a Problem with Minimum function, can any one help me?

**URL:** https://discourse.julialang.org/t/there-is-a-problem-with-minimum-function-can-any-one-help-me/87628
**Category:** General Usage
**Created:** [September 22, 2022, 9:28am UTC](https://discourse.julialang.org/t/there-is-a-problem-with-minimum-function-can-any-one-help-me/87628 "2022-09-22T09:28:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Alireza\_yousefi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alireza_yousefi/32/42860_2.png) [@Alireza\_yousefi](https://discourse.julialang.org/u/Alireza_yousefi)
#### Post date: [September 22, 2022, 9:28am UTC](https://discourse.julialang.org/t/there-is-a-problem-with-minimum-function-can-any-one-help-me/87628/1 "2022-09-22T09:28:03Z")

</div>

I wanna find the min & max of a column of a matrix. I use minimum function, but I have below problem, anyone know what happen?

![image](https://global.discourse-cdn.com/julialang/original/3X/7/1/7188f3b8e24acc7fd50dfe3848c8db6cc09885c9.png)

thanks

---

<div class="post-metadata">

### Author: ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)
#### Post date: [September 22, 2022, 9:31am UTC](https://discourse.julialang.org/t/there-is-a-problem-with-minimum-function-can-any-one-help-me/87628/2 "2022-09-22T09:31:08Z")

</div>

It means you have a NaN in your vector. See for example

```julia
minimum([1,NaN,2])

```

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [September 22, 2022, 10:04am UTC](https://discourse.julialang.org/t/there-is-a-problem-with-minimum-function-can-any-one-help-me/87628/3 "2022-09-22T10:04:27Z")

</div>

NaN stands for [Not a Number](https://en.wikipedia.org/wiki/NaN).

The problem is not in the call to the `minimum` function, but in the code that computed the `S_Elastic3` vector.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [September 22, 2022, 10:35am UTC](https://discourse.julialang.org/t/there-is-a-problem-with-minimum-function-can-any-one-help-me/87628/4 "2022-09-22T10:35:50Z")

</div>

You can do something like

```julia
julia> minimum(x for x ∈ [1, NaN, 2] if !isnan(x))
1.0

```

if you can’t change the fact that `NaN` values are occuring in your calculation but you’re still interested in the non-NaN minimum.

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [September 22, 2022, 12:05pm UTC](https://discourse.julialang.org/t/there-is-a-problem-with-minimum-function-can-any-one-help-me/87628/5 "2022-09-22T12:05:49Z")

</div>

> find the min & max of a column of a matrix

TL;DR you should rather do `using DataFrames` and use `describe`:

> Missing values are filtered in the calculation of all statistics, however the column :nmissing will report the number of missing values of that variable.

I’m relying on that whatever you do to import into it rather uses `missing` than `NaN`, because only the former is filtered. NaN can also happen in calculations, so isn’t strictly a good sign for missing, while I believe other languages e.g. R is it for that.

[https://dataframes.juliadata.org/stable/man/comparisons/](https://dataframes.juliadata.org/stable/man/comparisons/)

> Note that pandas skips `NaN` values in its analytic functions by default. By contrast, Julia functions do not skip `NaN`’s. If necessary, you can filter out the `NaN`’s before processing, for example, `mean(Iterators.filter(!isnan, x))`.
> 
> Pandas uses `NaN` for representing both missing data and the floating point “not a number” value. Julia defines a special value `missing` for representing missing data.

Depending on the cause of NaNs, e.g. if an artifact of importing, you can filter or substitute them somehow (also something like interpolating may apply): [Replacing \*missing\* and \*NaN\* values in dataframe - #2 by nilshg](https://discourse.julialang.org/t/replacing-missing-and-nan-values-in-dataframe/78687/2)

Simply replacing NaN (or `missing`) with 0 isn’t good advice (with `missing` likely better), but I noticed this blog post and it might be helpful: [https://www.roelpeters.be/replacing-nan-missing-in-julia-dataframes/](https://www.roelpeters.be/replacing-nan-missing-in-julia-dataframes/)

Older text: You can do that in one go, at least this way:

```julia
julia> A = [NaN 3; 4 missing]
2×2 Matrix{Union{Missing, Float64}}:
 NaN 3.0
   4.0 missing

julia> extrema(x for x ∈ skipmissing(A) if !isnan(x))
(3.0, 4.0)

```

About column of a “matrix”, it seems clear you’re referring to a table, and would want to be `using DataFrames` (or Pandas.jl).

I intentionally showed you could find extrema (or just e.g. minimum) of a full matrix (across columns), not just for one (or more) columns. You would want to slice one column (or row) at a time, as you know how to do. But I also looked a bit into doing that automatically for all each column.

I see you have the problem of `Vector{Any}` because of “Sc\_Young\_Modulus”. If you see `Any` (an abstract type, the top one; you can’t rely on the Abstract prefix, but I think that’s the major (only?) exception) like that, it’s likely going to kill performance. That’s one reason to want to use DataFrames or other way to skip header rows. You want to see concrete types, e.g. Vector of `Float64` for your whole column, and it also allows different types for each column without performance problems. Julia is unusual with this `missing` concept, which is similar to NaN, but more general since it works for all datatypes.

See “Handle Missing Data”, e.g. `dropmissing!` in the cheat sheet below.

I’m no expert on the package, so ~~I’m not sure if it has similar good functions~~ [EDIT: it seems as good] such as in Pandas:

[https://pandas.pydata.org/docs/getting\_started/intro\_tutorials/06\_calculate\_statistics.html](https://pandas.pydata.org/docs/getting_started/intro_tutorials/06_calculate_statistics.html)

While there is the Pandas.jl wrapper, that would work with all Julia data that’s compatible with Python, I doubt it supports `missing` (because Python can’t support it, also think the concept was introduced in Julia after that package). I’m not sure where you got NaN from, possibly an extra line when importing some data? You likely want to use CSV.jl to import. I’m not sure if it rather imports with `missing`, or possibly both it and NaN?

Because even though I showed how the avoid both, just checking for missing (if you can rely in that at most, or even avoid expecting that), is going to be much faster and allocate less, and simpler code:

```julia
julia> @time extrema(skipmissing(B))
  0.013557 seconds (11.08 k allocations: 612.520 KiB, 99.58% compilation time)

```

> **[DataFramesCheatSheet\_v1.x\_rev1.pdf](https://ahsmart.com/assets/pages/data-wrangling-with-data-frames-jl-cheat-sheet/DataFramesCheatSheet_v1.x_rev1.pdf)**
>
> 329.56 KB

If you used readdlm, and want to do minimal changes, then I would look into non-default options: header=true, comments=true, comment\_char=‘#’

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 21, 2024, 10:55pm UTC](https://discourse.julialang.org/t/there-is-a-problem-with-minimum-function-can-any-one-help-me/87628/6 "2024-08-21T22:55:52Z")

</div>

3 posts were split to a new topic: [Slow-running extrema in vsCode](https://discourse.julialang.org/t/slow-running-extrema-in-vscode/118469)
