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

조회 수: 4 (최근 30일)
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
Steven Lord
Steven Lord 2025년 6월 18일
To close the loop, would you mind giving a brief description of what the error was?

댓글을 달려면 로그인하십시오.

채택된 답변

Barbara Margolius
Barbara Margolius 2025년 6월 18일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by