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've run into a problem where I have an ordered array of sets of coordinates, for example:

OrderedArray = {{{70.8938, 216.539},{70.89,216.54}}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {{71.0656,216.573}}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {{67.6546, 220.338}}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {{70.9211, 216.364}}, {{70.9184, 216.346}}, {{70.9079, 216.349}}, {{70.9046, 216.335}}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {},{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {{70.951, 216.705}}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {{70.9621,216.586}}, {{70.918, 216.576}}, {{70.9116, 216.559}}, {{70.9189,216.581}}, {{70.9115, 216.565}}, {{70.9294, 216.552}}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {{67.0276, 218.154}}};

Note that some elements (here the first) contain more than a single coordinate.

And I need to fill in the blanks, i.e. the {} positions, via some interpolation procedure. What would be the best way to do this? Reading through the instructions for InterpolatingPolynomial (for example), and playing around a bit, it isn't immediately clear how to do this. Ideally I'd like to be able to specify that the "gaps" should be filled in assuming a linear curve, or a polynomial of some order.

share|improve this question
1  
"Note that some elements (here the first) contain more than a single coordinate." -- what do these represent? –  Mr.Wizard Jul 27 '13 at 8:48
    
Possible duplicate: (20994) –  Mr.Wizard Jul 27 '13 at 8:49
    
@Mr.Wizard These elements represents sets of coordinates for objects in particular frames of a movie. The blank {} spots represent the lack of an identified object in a frame. We're tracking the motion of an object from frame to frame with a sloppy identification technique that doesn't always work and sometimes falsely identifies multiple objects. –  Sparse Pine Jul 27 '13 at 8:49
    
Okay, but how are multiple coordinates to be handled? –  Mr.Wizard Jul 27 '13 at 8:52
    
@Mr.Wizard In the simplest case, the coordinates can be averaged, or a median can be calculated. However, I left this unspecified under the assumption that Mathematica' interpolation procedure could utilize the multiple data points for something like a least squares fit to the data. –  Sparse Pine Jul 27 '13 at 8:55
show 5 more comments

2 Answers

up vote 4 down vote accepted

I believe this may do what you desire:

f[x : {{_, _} ..}, y_] := {y, Mean@x}
f[{}, _] := Sequence[]

if = Interpolation[MapIndexed[f, OrderedArray], InterpolationOrder -> 1];

Array[if, Length@OrderedArray] // ListLinePlot

enter image description here

The filled array is produced by Array[if, Length @ OrderedArray].

share|improve this answer
    
Is there a way in which to remove points / edges that are longer than a certain length? –  Sparse Pine Jul 27 '13 at 9:28
    
I like the method a lot, but little bits of noise can really ruin the image. –  Sparse Pine Jul 27 '13 at 9:30
    
@SparsePine Sure, but that's probably best as a separate question, if it's not already somewhere on this site. –  Mr.Wizard Jul 27 '13 at 9:31
    
@SparsePine Thanks, don't be so quick to Accept an answer; it can discourage other, better, answers. I suggest all users wait 24 hours to let everyone have a chance to reply. –  Mr.Wizard Jul 27 '13 at 9:36
add comment

This is my approach:

Edit Sequence added in order to avoid Flatten. Also it seems to work only for V9.

 seg = Split@OrderedArray /. x : {{} ..} :> Length@x (*counting gaps*)
 seg = seg  /. y : {{_, _} ..} :> Mean@y // Flatten[#, 1] & (*averaging multiple points*)

 f[x : {_, _}, _] := x (* ordinary coordinates with no affect*)
 f[x_, {y_}] := Sequence @@ Array[# &, x + 2, seg[[{y - 1, y + 1}]] ][[2 ;; -2]]
                (* "counts"-> interpolation via Array*)
 MapIndexed[f, seg, {1}]
share|improve this answer
    
I'm getting errors from this, e.g. Array::plen: {48} and {{70.89,216.54},{71.0656,216.573}} should have the same length. >> –  Mr.Wizard Jul 27 '13 at 9:25
    
@Mr.Wizard we both have f, I had to reset kernel before looking at your method. –  Kuba Jul 27 '13 at 9:31
    
Believe it or not I'm familiar with symbol collisions and know how to avoid them. ;-) Like I said, I still have the problem, but I'll look into it. –  Mr.Wizard Jul 27 '13 at 9:32
    
Is this valid code in the version you are using? Array[#1 &, 48, {{70.89`, 216.54`}, {71.0656`, 216.573`}}] –  Mr.Wizard Jul 27 '13 at 9:35
1  
@Mr.Wizard That "4" is a typo - seems it will be fixed in V10 (to "9"). –  Michael E2 Jul 28 '13 at 3:01
show 6 more comments

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.