How do I delete an item or range from an array?

2,932 views
Skip to first unread message

Greg Harris

unread,
Feb 5, 2013, 10:20:18 PM2/5/13
to julia...@googlegroups.com
a = [11, 12, 13, 14]

In matlab, I could do something like this to remove some elements:
a[2:3] = []

Thanks

Viral Shah

unread,
Feb 6, 2013, 1:59:44 AM2/6/13
to julia...@googlegroups.com
That is very weird syntax that Matlab uses. We could probably provide a function that removes elements from a matrix, and it could do it in-place. 

-viral

Tim Holy

unread,
Feb 6, 2013, 6:08:51 AM2/6/13
to julia...@googlegroups.com
On Tuesday, February 05, 2013 10:59:44 PM Viral Shah wrote:
> That is very weird syntax that Matlab uses. We could probably provide a
> function that removes elements from a matrix, and it could do it in-place.

Greg, in case it's not obvious an easy way is this:

flag = trues(size(a))
flag[2:3] = false
a = a[flag]

That can easily be turned into a function, of course; as usual, the key
question is what to call it!

Matlab's syntax for this is convenient, but I agree with Viral that when you
think about it the syntax is rather strange.

--Tim

Greg Harris

unread,
Feb 6, 2013, 11:50:38 AM2/6/13
to julia...@googlegroups.com
Thanks, Tim.  Yeah, I figured this out after a while.  At first I guessed the delete! function would work on arrays.  Maybe it would be a good idea to extend its capabilities.

It is easy enough to write little helper functions for things I miss from Matlab (for example, a vectorized strcmp).  I wish, however, that there was a way to make my little generic functions available to all my code in every session.  I suppose I could bundle them together in a package that I import using the Julia startup script.  But then they would clutter my workspace when I type whos().  Any ideas?

Thanks,

Greg

Tom Short

unread,
Feb 6, 2013, 11:54:37 AM2/6/13
to julia...@googlegroups.com
If the package has a surrounding module, then those functions won't
clutter whos().

Viral Shah

unread,
Feb 6, 2013, 12:00:42 PM2/6/13
to julia...@googlegroups.com
The delete name does suggest that it would do this, but it does not.

-viral

Viral Shah

unread,
Feb 6, 2013, 12:19:07 PM2/6/13
to julia...@googlegroups.com
We have delete! which deletes values at a specified index, and filter! which can remove values from an array.

Jacob Quinn

unread,
Feb 6, 2013, 12:41:11 PM2/6/13
to julia...@googlegroups.com
On a related note, is there currently a generic 'remove' function to purge objects from the environment? I'm used to R's "remove" and "rm" functions..........

-Jacob

Greg Harris

unread,
Feb 6, 2013, 12:42:26 PM2/6/13
to julia...@googlegroups.com
It looks like I was getting mixed up with other languages.  I was typing: del(A, [2:3]), which doesn't work.  Anyway, the following seems to work for in-place deletion from an array:

A = [11, 12, 13, 14]
del(A, 2:3)

Viral, on my machine, delete! isn't actually defined.  Is it just an alias to del?  Do I need to load something to see it?  Or, is my installation too old/new?

Thanks,

Greg

Stefan Karpinski

unread,
Feb 6, 2013, 12:51:37 PM2/6/13
to Julia Users
You may have an older version of Julia. The del function has been deprecated in favor of delete!.
Reply all
Reply to author
Forward
0 new messages