Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
5  
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
add comment

1 Answer

up vote 6 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
add comment

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.