odehybrid
by An Uncommon Lab

state_to_vector

Build a state vector by recursively moving through the elements of a more complex state (matrix, cell array, or struct), saving the numerical values along the way in a vector. This is used in odehybrid to convert a complex series of states into a vector to be used for continuous state updates. The reverse function is vector_to_state.

vector = state_to_vector(state);

Inputs

stateA numeric, cell array, or struct array type, the contents of which consist of numeric, cell array, or struct array types, etc.

Outputs

vectorA vector containing the numerical values from the state (doubles)

Example

state1 = [1 3; 4 2];
state2 = struct('a', {5; 6}, 'bcd', {7:9; 10});
state3 = pi;
x = {state1, state2, state3};
v = state_to_vector(x)
v =
    1.0000
    4.0000
    3.0000
    2.0000
    5.0000
    7.0000
    8.0000
    9.0000
    6.0000
   10.0000
    3.1416
x2 = vector_to_state(v, x)
x2 = 
    [2x2 double]    [2x1 struct]    [3.1416]
isequal(x, x2)
ans =
     1

See Also

vector_to_state