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

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 个评论

Can you show us the whole class method definition? I'm wondering if you have defined a variable in the method that's shadowing a function used by that code.
You are right, I'm sure. A stripped down version of the class works correctly, so isn't useful for illustrating the problem. The class itself is too long to post. I'll try to build it back up to find the error. Thanks.
Perhaps you can add the command whos to the code immediately before the place where you included these lines of code and show us that output. That may indicate if you've shadowed a function with a variable.
plot = 42;
y = digraph();
z = @sin;
whos
Name Size Bytes Class Attributes plot 1x1 8 double y 1x1 9 digraph z 1x1 32 function_handle
% plot(1:10) % would not work due to the shadowing
Since the standalone code behaves as expected and the issue arises only within your class, it is very likely that a variable name inside your method or class is shadowing a built-in function like reshape, cos, or pi.
To diagnose this, please try adding the following just before the line where A1(tt) is evaluated in your class method:
disp('--- Function shadow check ---');
disp('reshape:'); which reshape
disp('cos:'); which cos
disp('pi:'); which pi
disp('--- Variables in scope ---');
whos
Then run your method and paste the output here. This will help determine whether a variable or property is unintentionally overriding a built-in function and causing the unexpected 2×800 result.
To close the loop, would you mind giving a brief description of what the error was?

请先登录,再进行评论。

 采纳的回答

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 个)

类别

帮助中心File 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