Matrix of NaNs

2,831 views
Skip to first unread message

StephenLL

unread,
Jul 13, 2013, 3:21:42 PM7/13/13
to julia...@googlegroups.com
Long time matlab user here.

I can't find the equivalent command in julia to produce a matrix of NaNs or for that matter a matrix filled with a single value.

What is the generally accepted way of doing this?  I can think of using ones()*NaN or even repeat(NaN,) but was not sure if there was a better way or another generally accepted way.

Thanks.

FYI     I am in the middle of implementing a bootstrap based simulation algorithm that I have in matlab in c#, R, and Julia. When I am done I will report back my finding.  Specifically looking at speed and how easy it is to write maintainable and extensible OOP based code.

Viral Shah

unread,
Jul 13, 2013, 3:39:55 PM7/13/13
to julia...@googlegroups.com
You could do:

a = Array(Float64, size)
fill!(a,NaN)

or use a comprehension.

-viral

Stefan Karpinski

unread,
Jul 13, 2013, 4:04:54 PM7/13/13
to Julia Users
Can be written as a one-liner:

a = fill!(Array(Float64,m,n),NaN)

comprehension version:

[ NaN for i=1:m, j=1:n ]

Tim Holy

unread,
Jul 13, 2013, 4:13:55 PM7/13/13
to julia...@googlegroups.com
Since we have zeros(), ones(), trues(), and falses(), should we just add
infs() and nans() too?

--Tim

Stefan Karpinski

unread,
Jul 13, 2013, 4:15:36 PM7/13/13
to Julia Users
Seems reasonable to me.

Tim Holy

unread,
Jul 13, 2013, 4:30:35 PM7/13/13
to julia...@googlegroups.com
On Saturday, July 13, 2013 04:15:36 PM Stefan Karpinski wrote:
> Seems reasonable to me.

Done.

--Tim

Stefan Karpinski

unread,
Jul 13, 2013, 4:32:15 PM7/13/13
to Julia Users
Bah. You beat me to it. We probably made identical changes.

Simon Kornblith

unread,
Jul 13, 2013, 4:35:18 PM7/13/13
to julia...@googlegroups.com
There's also

a = fill(NaN, m, n)

Stefan Karpinski

unread,
Jul 13, 2013, 4:39:30 PM7/13/13
to Julia Users
Ah, right. Even better.

Tim Holy

unread,
Jul 13, 2013, 8:57:19 PM7/13/13
to julia...@googlegroups.com
On Saturday, July 13, 2013 04:32:15 PM Stefan Karpinski wrote:
> Bah. You beat me to it. We probably made identical changes.

I saved twos() and threes() for you :-)



More seriously, I forgot to include help text. If you did, go ahead (otherwise
I will tomorrow).
Reply all
Reply to author
Forward
0 new messages