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.

What is the equivalent of MATLAB's "timealign" function?

Given two sequences (lists of values) and offsets, timealign will insert 0's into sequences to align them so they have the same number of elements and are thus aligned for the purpose of doing operations across both sequences in parallel.

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

Assuming it's this timealign

Here's a way to do it using rules, the input is slightly altered and now takes {t1, u1}, {t2, u2}, ... as input instead of t1, t2, u1, u2. Null is used instead of NaN, the time indices don't have to be ordered but will be in the result:

timeAlign[pairs_List] :=
 With[{
   t = Union @@ pairs[[All, 1]]
   },
  Prepend[
   Replace[t,
     Append[
      Thread[First@# -> Last@#], _ -> Null
     ]&/@pairs,
     {1}],
   t]
  ]
timeAlign[pairs__List] := timeAlign[{pairs}]

timeAlign[
 {{3, 2, 1}, {8, 7, 6}},
 {{2, 4, 5}, {{3, 4}, {-1, 2}, {9, 12}}},
 {{1, 5, 8}, {a, b, c}}
] // TeXForm

$$\left( \begin{array}{cccccc} 1 & 2 & 3 & 4 & 5 & 8 \\ 6 & 7 & 8 & \text{Null} & \text{Null} & \text{Null} \\ \text{Null} & \{3,4\} & \text{Null} & \{-1,2\} & \{9,12\} & \text{Null} \\ a & \text{Null} & \text{Null} & \text{Null} & b & c \\ \end{array} \right)$$

share|improve this answer
    
I guess I will "accept" this as an answer, if by this complicated formula you mean to say that Mathematica has no equivalent of timealign unless you program it yourself. –  Tyler Durden Oct 29 '13 at 16:39
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.