Construction of a Matrix from columns via array comprehension

875 views
Skip to first unread message

Paweł Biernat

unread,
Apr 2, 2014, 8:53:52 AM4/2/14
to julia...@googlegroups.com
Hi,
I am trying to construct a matrix (2d array) via the array comprehension and hcat like this

y=[1 for i=1:3, j=1:3]
yy=hcat(y[:,1:2],[y[:,j] for j=2:3])

which fails with the following message

ERROR: mismatch in dimension 1
 in cat_t at abstractarray.jl:680
 in hcat at abstractarray.jl:719
 in anonymous at no file

I understand, that this message is caused by the difference in types

julia> typeof(y[:,1:2])
Array{Int64,2}

julia> typeof([y[:,j] for j=2:3])
Array{Any,1}


How can I inform Julia of that fact that the result of array comprehension is a 2d array? I tried to add some assertion to the list comprehension like this
hcat(y[:,1:2],Array{Int64,1}[y[:,j] for j=2:3])

but it still doesn't work.

Alternatively, is there any other way to prepend/append/insert a column/row to the matrix y?

Andreas Noack Jensen

unread,
Apr 2, 2014, 9:30:58 AM4/2/14
to julia...@googlegroups.com
Maybe this is what you want

[y[:,1:2] hcat([y[:,j] for j=2:3]...)]
--
Med venlig hilsen

Andreas Noack Jensen

Paweł Biernat

unread,
Apr 2, 2014, 10:41:45 AM4/2/14
to julia...@googlegroups.com
W dniu środa, 2 kwietnia 2014 15:30:58 UTC+2 użytkownik Andreas Noack Jensen napisał:
Maybe this is what you want

[y[:,1:2] hcat([y[:,j] for j=2:3]...)]


Yep, this is what I was after.  Thanks!

What does "..." do?  I couldn't find it in the documentation.

Alan Edelman

unread,
Apr 3, 2014, 9:39:53 AM4/3/14
to julia...@googlegroups.com
It splices the arguments
Turns out to be really handy

Ethan Anderes

unread,
Apr 3, 2014, 9:58:51 AM4/3/14
to julia...@googlegroups.com
Indeed, ... is very handy and I also missed it in the documentation. Would it make sense to add more documentation for ... in the section that introduces optional arguments (since it makes the syntax f(optional_args..) more clear)?

Maybe also add it to the docs for hcat, where I use it all the time??

Ethan

Paweł Biernat

unread,
Apr 3, 2014, 12:23:19 PM4/3/14
to julia...@googlegroups.com
So is it like a Sequence from Mathematica?

Sequence works like this:
In[1]: f[a, Sequence[b, c], d]
Out[1]: f[a, b, c, d]

Ethan Anderes

unread,
Apr 3, 2014, 4:10:08 PM4/3/14
to julia...@googlegroups.com
I don't know mathematica but it looks like ... does the same thing. I use it often when I have a function that has a lot of arguments.

function foo1(x,y,z,w)
sin(x+y+z+w)
end

fin = [
3,
4,
5,
6
]

foo1(fin...)

It also works for keyword args

function foo2(;x = 1, y = 2, z = 3, w = 4)
sin(x+y+z+w)
end

fin = [
:x => 3,
:y => 4,
:w => 5,
:z =>6
]

foo2(;fin...)

Cool, right?

Paweł Biernat

unread,
Apr 3, 2014, 6:55:45 PM4/3/14
to julia...@googlegroups.com
That's very impressive.  I used Sequence in Mathematica a lot and its good to know Julia implements similar functionality.  What is the overhead for "..."?  For example in the case of hcat([i for i=1:3]...).  Is it going to be as fast as writing the arguments explicitly like in hcat(1,2,3)?

Stefan Karpinski

unread,
Apr 5, 2014, 9:30:51 AM4/5/14
to Julia Users
That depends very much on whether Julia can figure out exactly how many elements are in the array that's being splatted or not. The type of structure you're splatting matters too. In general, you should avoid it where possible, especially constructing an array and then splatting it into a function call. Splatting multiple return values (i.e. a tuple) from a function into the argument list of a function call is often ok, however.
Reply all
Reply to author
Forward
0 new messages