How Delete row or col of array? like deleteat! in vectors

2,330 views
Skip to first unread message

paul analyst

unread,
Jun 16, 2014, 2:36:16 PM6/16/14
to julia...@googlegroups.com
a=rand(5,5)

how Delete row(s) or col(s) of array ?

Paul

Stefan Schwarz

unread,
Jun 16, 2014, 3:22:52 PM6/16/14
to julia...@googlegroups.com
deleting in place a matrix is not supported and does not make sense.

why is that?

you've for instance a 5x5 matrix and you want to delete an item.
deleteat! shifts subsequent items to fill the resulting gap, so you're
loosing dimensionality.

basically what you can do is this:

    a = deleteat!(a[1:end], 1)

which is deleting item at position 1.

if you want to bring that back into a matrix representation the problems rear up.

what dimension you'll have afterwards? 6x4?
if you delete another one you'll have 23 elements left, which is really hard to bring
into a 2D shape.

in the end you've to come up with your own scheme with deleting an 
item at (i, j).

deleting an item and presuming deleting means zeroing it out:

    a[2,2] = convert(eltype(a), 0)

deleting the 2nd column:

    a[:,2] = convert(eltype(a), 0)

deleting the 1st row:

    a[1, 1:end] = convert(eltype(a), 0)

don't know if this tackles your question, but hope this helps.

Paul Analyst

unread,
Jun 17, 2014, 1:50:04 AM6/17/14
to julia...@googlegroups.com
Thx, for this info
But not about items I need delete some rows ...

How fast delete rows in arrray, one [3,:]or more [[2,4],:] ?

Paul

W dniu 2014-06-16 21:22, Stefan Schwarz pisze:

Paul Analyst

unread,
Jun 18, 2014, 5:26:50 AM6/18/14
to julia...@googlegroups.com
Is possible or not ? Delete rows in array ?
Paul
W dniu 2014-06-17 07:49, Paul Analyst pisze:

Tim Holy

unread,
Jun 18, 2014, 6:53:42 AM6/18/14
to julia...@googlegroups.com
A = rand(5,5)
A = A[[1:2,3:5], :]

--Tim

Paul Analyst

unread,
Jun 18, 2014, 7:29:22 AM6/18/14
to julia...@googlegroups.com
number removal line is dynamic. This time 4 then maybe 2 maybe 3 etc

Question now :
How to construct fromvector [1:10] new vector without [2,4] like this
[1,3,5:10]

Paul


W dniu 2014-06-18 12:53, Tim Holy pisze:

Tim Holy

unread,
Jun 18, 2014, 7:38:22 AM6/18/14
to julia...@googlegroups.com
setdiff

--Tim

Paul Analyst

unread,
Jun 18, 2014, 8:41:06 AM6/18/14
to julia...@googlegroups.com
Ok, txh,

A have lot of long vectors. Is it good time or look for something faster?

julia> a=[1:1:10^6];

julia> b=[1:10:10^6];

julia> @time setdiff(a,b)
elapsed time: 1.339431633 seconds (252659008 bytes allocated, 14.36% gc
time)
900000-element Array{Int64,1}:

Paul

W dniu 2014-06-18 13:38, Tim Holy pisze:

Tim Holy

unread,
Jun 18, 2014, 8:51:03 AM6/18/14
to julia...@googlegroups.com
If you left them as ranges rather than converting them to vectors (if that fits
your usual usage case), it would be possible to have a special version of
setdiff operating on ranges that would be much faster. Would be great if you
could write & submit that.

--Tim

paul analyst

unread,
Jun 18, 2014, 9:06:24 AM6/18/14
to julia...@googlegroups.com
Thanks, but I'm an analyst market, not a programmer;/ Still, I really like Julia. Historically, only SPSS. I can so deeply programmed? Any tips?
Paul

Tim Holy

unread,
Jun 18, 2014, 10:51:53 AM6/18/14
to julia...@googlegroups.com
On Wednesday, June 18, 2014 06:06:24 AM paul analyst wrote:
> Thanks, but I'm an analyst market, not a programmer;/ Still, I really like
> Julia. Historically, only SPSS. I can so deeply programmed? Any tips?

Yes, with Julia it's easy to reach in "deep." First you might want to look at
existing methods for setdiff:

julia> methods(setdiff)
# 3 methods for generic function "setdiff":
setdiff(a::IntSet,b::IntSet) at intset.jl:90
setdiff(a::Set{T},b::Set{T}) at set.jl:73
setdiff(a,b) at array.jl:1352

From this, personally I'd guess the implementation in array.jl will be closest
to what you want. So open up an editor and look at the implementation in
base/array.jl (I'll add a few comments to help you):

# from array.jl, line 1352:
function setdiff(a, b)
# The first few lines initialize an empty output Vector
# with the correct element type,
# and convert b to a Set so that it's fast to test whether
# an element of a can be found in b.
args_type = promote_type(eltype(a), eltype(b))
bset = Set(b)
ret = Array(args_type,0)
# seen is to make sure we return just the unique values in a
seen = Set()
for a_elem in a # loop over all the elements in a
# test that we haven't seen a_elem before,
# and that it's not present in b
if !in(a_elem, seen) && !in(a_elem, bset)
push!(ret, a_elem) # keep the element
push!(seen, a_elem) # mark it as seen
end
end
ret # return the result
end

That's it!


For implementing your own version, start like this:

import Base.setdiff

function setdiff(a::Ranges, b::Ranges)
# code goes here
end

This means your version will be called whenever both inputs are Range-type
objects.

For the actual implementation, here's another hint: you probably noticed that
most of the "magic" was taken care of by the "in" function. Why not check to
see whether there are any methods already defined for Range objects? If so, you
can expect that they might be more efficient that for Sets. Also, you probably
don't have to worry about "seen" because the elements of the range are
guaranteed to be unique (if step(a) != 0). So, you could almost cut-and-paste
that implementation for arrays, but delete all the parts that deal with Sets.

Once you've developed the code, try posting it here for comments. Eventually
you might want to learn git, but let's take one step at a time :-).

--Tim

Paul Analyst

unread,
Jun 18, 2014, 12:23:34 PM6/18/14
to julia...@googlegroups.com
Great lesson thanks, I'll try :)
Paul

W dniu 2014-06-18 16:51, Tim Holy pisze:
Reply all
Reply to author
Forward
0 new messages