0

I am a python (and numpy) regular who just started using Matlab. I am solving boundary value problems using bv4pc, and I want to store the result arrays, solved for different parameters, in a larger array.

For example, consider the following code that returns a result vector y for each value of the parameter t -

for j = 1:loops
    t = 1/(sqrt(2).^j)
    % solve ODE that depends on parameter t
    sol = bvp4c(@(x,y)elasticaODE(x,y,t),@(ya,yb)elasticaBC(ya,yb,t),solinit,options);

    % The solution at the mesh points
    x = sol.x;
    y = sol.y;

In python, I would just do:

yVector = []
for (t in tArray):
    ... solve ODE, get y ...
    yVector.append(y)
yVector = np.array(yVector)

What is the equivalent in Matlab?

ap21
  • 2,372
  • 3
  • 16
  • 32

1 Answers1

0

In Matlab, most variables are matrices by default. There are two exceptions that might be what you are looking for:

1- Cells

Cells are similar to Python arrays, in the sense that each element can be anything

my_cell = {'a', 1, "wut", [1,2,3]};

And you can access the elements using the curly braces notation:

my_cell{1} % yields 'a'

my_cell{4}(2) % yields 2

2-Struct Arrays

You can define a struct by declaring it's fields, and they are mutable, a bit like in a Python dictionary:

my_struct.a = 1 my_struct.b = 2

If you have structs with the same fields, you can create a vector of structs:

my_second_struct.a = 3 my_second_struct.b = 4

my_vector_of_structs = [my_struct; my_second_struct]

Again, each field in a struct can be anything, even another struct.

Mefitico
  • 816
  • 1
  • 12
  • 41