5
$\begingroup$

Suppose I have an array like so

array1 = {{Null,2,3,4,5},
         {a,b,c,Null,e},
         {w,x,Null,y,z}}

Is there some way to selectively remove any element I want? In this case, I want to remove any Null element so that I get

array2 = {{2,3,4,5},
         {a,b,c,e},
         {w,x,y,z}}

I attempted this by trying out the Complement command. As an example, I did something to the effect of the following.

array3 = {{1,1,Null},
          {2,Null,Null},
          {a,a,Null}}

null = {Null}

newarray = Complement[array3,null]

which gives

newarray = {{1},
            {2},
            {a}}
$\endgroup$
5
$\begingroup$
array1 /. Null -> Sequence[]
(* {{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}} *)

Replace[array1, Null :> Sequence[], {2}]
(* {{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}} *)

DeleteCases[array1, Null, {2}]
(* {{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}} *)

SetAttributes[foo, Listable]
foo[Null] = Sequence[];
foo[x_] := x
foo@array1
(* {{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}} *)
$\endgroup$
2
$\begingroup$

A minor variation of @kglr's DeleteCases approach is to use a negative level spec targeting leaves only:

DeleteCases[array1, Null, {-1}]

{{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}}

$\endgroup$
0
$\begingroup$

This will do it.

array1 = {{Null, 2, 3, 4, 5}, {a, b, c, Null, e}, {w, x, Null, y, z}};
array1 /. {h___, Null, t___} :> {h, t}

which then gives you

{{2, 3, 4, 5}, {a, b, c, e}, {w, x, y, z}}
$\endgroup$
  • $\begingroup$ Ah, perfect. I was familiar with the /. symbol but I wasn't sure how to use it properly. EDIT: does the number of underscores set a certain radius for which elements in the list to consider for deletion? $\endgroup$ – user170231 Oct 30 '14 at 3:20
  • $\begingroup$ Buying an old used book that explains how to use some of the features of Mathematica will save you a lot of time. Long ago I learned a lot from reading and rereading "Applied Mathematica: Getting Started, Getting it done." That is where I learned /. and # and & and all that stuff. That is ancient now and used copies are often a dollar. That won't teach you animating your manipulate in the cloud with natural language using subscripts, but it will teach you the fundamentals you will use over and over. Mathematica Navigator older editions are cheap used and would be good too. $\endgroup$ – Bill Oct 30 '14 at 3:25
  • $\begingroup$ x_ (1 of them) matches a "single thing", but a thing can also be a list, so watch out. x__ (2 of them) matches "one or more things." x___ (3 of them) matches "zero or more of them." So my pattern above was "start a list and then have zero or more things, call those h and then have a Null and then have zero or more things, call those t and end the list, then replace that with the list that is just h followed by t. Now it is time for you to think. What would this do if you had more than one Null in one or more of your lists? What if I only used 2 _? What if I only used 1 _? Try this. Learn. Go. $\endgroup$ – Bill Oct 30 '14 at 3:30
  • $\begingroup$ I see. The reason I ask is that the arrays I'm using a much larger and some rows didn't get "Null" removed. I'll do some experimenting. Thanks for the help $\endgroup$ – user170231 Oct 30 '14 at 5:00
  • $\begingroup$ Compare a few of the rows that didn't get Null removed with those that did. See if you notice anything about the first group that differs from the second group. I have a hunch, but without data can't be sure. What I showed was one way that worked with your example. Sometimes after getting an answer people might realize that wasn't really the question they should have asked. You might find some of your rows differ from your example and need a different answer. Note that with Mathematica there are always a dozen different ways of doing anything, at least a couple of which are incomprehensible. $\endgroup$ – Bill Oct 30 '14 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.