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.

I am 'Map'ping over a list. I would like to do the computation in one single iteration. Is it possible? Can 'Apply' be used here? In what way is it different from 'Map' especially in this case?

eventnindicators= {{10,0,1},{11,1,1},{15,0,1},{100,1,2},{9,0,2},{10,1,2},{105,0,2}};
censorIndicators= {0,1,0,1,0,1,0};
expectedcnt1 = Function[{c}, 
  Length@Pick[censorIndicators, Sort[eventnindicators], _?(First[#] >= First[c] &)]
 ] /@ Union[eventnindicators];

Is that too much of code? If yes, please let me know the lines to remove. I am looking for a very general idea to make this faster.

The gist is that I try to do a number of 'Picks' and 'Counts' using #, where I iterate over each of the elements INSIDE the 'Map' function, which actually iterates over a list itself. Is there anyway to cutdown these iterative processes? Is it possible to 'Pick' or 'Count' without the # s? Also, is it possible to not use 'Map' at all here?

Thanks.

share|improve this question
6  
Yes, it's too much code without any explanations. Try to find a minimal example that shows your issue, so that most of the effort we put is in actually answering your doubts and not in decyphering code. Or explain what it does in a few words if this is not possible –  Rojo Nov 23 '12 at 21:49
    
I am pretty sure @Rojo did not intend with his comment to ask you to describe in prose a few of the operations used in the code. We all know Mathematica and can mechanically follow the code, but still need help in grasping your overall intent. You asked for a general solution, so give us a general problem. At a high level, what operation do you seek to accomplish? –  Oleksandr R. Nov 24 '12 at 2:24
    
I have edited it further. Now, as I asked, I am not paraphrasing what my code does, I am asking for a solution to remove/minimize the iterations. Any ideas? Thanks –  preeti Nov 24 '12 at 2:28
    
@preeti is there a particular reason you use N[#,5]? In the given example it does not matter, and, if possible, it'd be nice to remove excess clutter. Also, why are you using Return at all? –  VF1 Nov 24 '12 at 3:33
2  
This bears no resemblance to the original algorithm, but its current output can be reproduced by Reverse@Accumulate@Flatten[# UnitVector[#, 1] & /@ Reverse@Tally[Sort[eventnindicators[[All, 1]]]][[All, 2]]]... is that really the kind of thing that you wanted? –  Oleksandr R. Nov 24 '12 at 3:45
show 2 more comments

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.