4
$\begingroup$

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.

$\endgroup$
0

1 Answer 1

3
$\begingroup$

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)$$

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ Oct 29, 2013 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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