Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sorry for the most basic of questions. How would I find the positions of a list of elements which are empty, for example I have the list

a = {1, 2, {}, {}, 3, 5};

and I want to find the indices of the empty elements.

share|improve this question
6  
Position[{1, 2, {}, {}, 3, 5}, {}] yields {{3}, {4}} – Artes Jun 13 '13 at 11:02
5  
Additionally you can get all elements of the list that are not {} using Cases[{1, 2, {}, {}, 3, 5}, Except[{}]]. – mmal Jun 13 '13 at 11:25
1  
This is better DeleteCases[{1, 2, {}, {}, 3, 5}, {}]. – Artes Jun 13 '13 at 12:55
    
@Artes I would say its equivalent. – mmal Jun 13 '13 at 15:05
    
@mmal It is not. – Artes Jun 13 '13 at 15:47
up vote 8 down vote accepted

If you want to find the position of something in a given expression, you should use Position.

In this example, you could simply write:

Position[{1, 2, {}, {}, 3, 5}, {}]
(*{{3}, {4}}*)

I have to mention that this not the whole power of Position: the second parameter of Position can be a "pattern"(which may be not known by a matlab coder:)), look up the doc for more imformation.

share|improve this answer
    
Great thanks, I know a little about regexp – mor22 Jun 13 '13 at 11:28
1  
@mor22 a Pattern in Mathematica is different from a regular expression. They're similar in some respects, but a pattern is more broad. – rcollyer Jun 13 '13 at 12:11
    
@rcollyer Thanks, I'm still learning! – mor22 Jun 14 '13 at 7:22
    
Thank you, this is helpful. In some cases, i.e. when importing data, one might need to use {""} instead of {}. – Doron Klepach Sep 13 '15 at 9:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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