Matlab Cell Array Replacement

1,008 views
Skip to first unread message

thomaslai

unread,
Jun 29, 2012, 4:28:32 PM6/29/12
to juli...@googlegroups.com
I was porting my matlab code and was wondering what is the best way to replace the functionalities provided by the matlab cell arrays. The context is as belows:

I have a custom type opKron that takes in multiple objects of another custom type opMatrix, and the constructor should look like this, opKron( opMatrix, opMatrix,.... ) with as many opMatrices as the user desires. For this i need three functionalities:

  1. The ability to accept variable number of input arguments.
    • Normally I just have to use opKron(varargin) in matlab. Now I know that opKron(op1,ops...) works in Julia but then this gives me op1 of type opMatrix and ops which is a tuple. This breaks my cell array into ungainly pieces which brings us to the next problem.

  2. The ability to "uncell" and recombine
    • Normally in matlab all I need to do is: newOpsArray = {op1, ops{:} } and I will get a new non-nested cell array, emphasis on non-nested, as I need to index into this array for future processing. In Julia I have tried all possible symbols and functions to "uncell" the tuple and scoured all possible documentation for any hints but this uncellibility and unconcatenatibility of tuples still vex me. When I do newOpsArray = op1, ops, all i get is something like (A, (A,A,A,A,) ) which is highly undesirable. Please advise.

  3. The abilility to extend cell arrays when needed
    • I need to do opsArray{end+1} = A, how do i do this in Julia?

Avik Sengupta

unread,
Jun 29, 2012, 4:40:00 PM6/29/12
to juli...@googlegroups.com
The following seems to work... is this what you need? 

julia> a="A"
"A"

julia> b=("B", "C")
("B","C")

julia> [a;b...]
3-element ASCIIString Array:
 "A"
 "B"
 "C"

To add to the end of an array, use push

julia> c=["C";"D"]
2-element ASCIIString Array:
 "C"
 "D"

julia> push(c, "E")
3-element ASCIIString Array:
 "C"
 "D"
 "E"

HTH
-
Avik

Stefan Karpinski

unread,
Jun 29, 2012, 4:58:23 PM6/29/12
to juli...@googlegroups.com
Using tuples for this is not ideal (they're immutable and doing appending will cause things to be very, very slow). What you want is an Array{Any}, which can be constructed using curly brace literals:

julia> a = {}
0-element Any Array

julia> push(a,1)
1-element Any Array:
 1

julia> push(a,"foo")
2-element Any Array:
 1     
  "foo"

Array in general use an exponential allocation algorithm so growing them in a loop is better than O(n^2), but it's still *much* better if you can pre-allocate the right array size and assign elements without growing. Note that Julia does not support implicit array growing by assigning past the end of an array; you need to explicitly grow the array.

Jeff Bezanson

unread,
Jun 29, 2012, 7:01:14 PM6/29/12
to juli...@googlegroups.com
In general, the equivalent of a{:} in matlab is "a...". {op1, ops...}
should work.

Tim Holy

unread,
Jun 29, 2012, 7:19:06 PM6/29/12
to juli...@googlegroups.com
On Friday, June 29, 2012 01:28:32 PM thomaslai wrote:
> I was porting my matlab code and was wondering what is the best way to
> replace the functionalities provided by the matlab cell arrays.

As others have said, cell arrays in Matlab are Arrays of type Any in Julia.
While the {a,b} syntax will surely be familiar to you, be aware that you index
them with [] just like all other arrays in Julia (i.e., not like Matlab's ()
for arrays and {} for cell arrays).

> The context
> is as belows:
>
> I have a custom type opKron that takes in multiple objects of another
> custom type opMatrix, and the constructor should look like this, opKron(
> opMatrix, opMatrix,.... ) with as many opMatrices as the user desires. For
> this i need three functionalities:
>
>
> 1. The ability to accept variable number of input arguments.
> - Normally I just have to use opKron(varargin) in matlab. Now I know
> that opKron(op1,ops...) works in Julia but then this gives me op1 of
> type opMatrix and ops which is a tuple. This breaks my cell array into
> ungainly pieces which brings us to the next problem.

Try opKron(ops::opMatrix...)
Then the whole argument list will be a single tuple

>
> 2. The ability to "uncell" and recombine
> - Normally in matlab all I need to do is: newOpsArray = {op1, ops{:}
> } and I will get a new non-nested cell array, emphasis on non-nested,
> as I need to index into this array for future processing.

To amplify further on Jeff's points:

julia> c1 = "Hello"
"Hello"

julia> c2 = ("how", "are", "you")
("how","are","you")

julia>

julia> c = [c1, c2...]
4-element ASCIIString Array:
"Hello"
"how"
"are"
"you"

julia> typeof(c1)
ASCIIString

julia> typeof(c2)
(ASCIIString,ASCIIString,ASCIIString)

julia> typeof(c)
Array{ASCIIString,1}

julia> cc = {c1, c2...}
4-element Any Array:
"Hello"
"how"
"are"
"you"

julia> typeof(cc)
Array{Any,1}


> In Julia I have
> tried all possible symbols and functions to "uncell" the tuple and scoured
> all possible documentation for any hints but this uncellibility and
> unconcatenatibility of tuples still vex me.

It took me a while too. At one point I started writing some exercises (plus
answers) for the manual, I should try to get those merged.

--Tim

thomaslai

unread,
Jun 29, 2012, 8:00:57 PM6/29/12
to juli...@googlegroups.com
Awesome answers, thank you guys!
Reply all
Reply to author
Forward
0 new messages