defining function which generates 3d array within a class. Different behavior in class versus command window

3 visualizzazioni (ultimi 30 giorni)
This code:
lam = @(t) 3*(1 + 0.8*cos(2*pi*t));
A1 = @(t) reshape([lam(t(:).'); lam(t(:).'); zeros(1, numel(t)); lam(t(:).')], [2, 2, numel(t)]);
tt=0:1/400:1-1/400;
A1stack=A1(tt);
generates a 2x2x400 array. This is what I want to happen. When I embed the code within a class function however, it returns a 2x800 array. How do I fix this?
  6 Commenti

Accedi per commentare.

Risposta accettata

Barbara Margolius
Barbara Margolius il 18 Giu 2025
I have a matrix function that is periodic. I need to use it in two ways: as a building block for an infinitesimal generator with block tri-diagonal structure: to be used to solve an ODE for the truncated system. I also wanted to use the same function to obtain the values of at values of t given by the vector tt, taking advantage of vectorization. So the code
lam = @(t) 3*(1 + 0.8*cos(2*pi*t));
A1 = @(t) reshape([lam(t(:).'); lam(t(:).'); zeros(1, numel(t)); lam(t(:).')], [2, 2, numel(t)]);
tt=0:1/400:1-1/400;
A1stack=A1(tt);
works very nicely for providing a 3D array of values for , but the definition conflicted with what I needed for the infinitesimal generator. I ended up defining both the 3D and 2D versions of the function. Defining it twice presents an opportunity for error in that the definitions need to be consistent to make sense.
The error was that I told matlab that A1 was a matrix at one point in the code(so I could use it in defining the generator), but that wasn't true.

Più risposte (0)

Prodotti


Release

R2025a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by