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

5 vues (au cours des 30 derniers jours)
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 commentaires
Steven Lord
Steven Lord le 18 Juin 2025
To close the loop, would you mind giving a brief description of what the error was?

Connectez-vous pour commenter.

Réponse acceptée

Barbara Margolius
Barbara Margolius le 18 Juin 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.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by