Chain array indexing error to access elements of a function

I keep getting an error in trying to access elements of an array that is defined as a function. Consider the foloowing function that demonstrates my issue:
test = @(t) [t, t^2];
which takes in the time parameter, t, and returns a 1x2 array of floats that depend on that value of t. Now, I want to define a function, x = @(t) to be the first element of the 1x2 array returned by test(t).
How can I do this? I have tried the following:
x = @(t) test(t)(1). However, this doesn't work. Specifically, Matlab considers (t)(1) to be a nested index, which is strictly not allowed. The error message returned by Matlab is "Invalid array indexing or function call. Chaining outputs after parenthesis is not supported".
How can I achieve what I described?
Note that test is an example function, my actual function is a little more involved.
EDIT (Additional info based on answers)
Based on answer I received, it is worth clarifying that I do not have access to the function x(t). I.e. I am working with vectors that define the solution, which in our above example would read
test = @(t) [ x(t), y(t), z(t) ] % This step is yielding after some pre-calculation that bears no signifcance to the question
The rest of the calculation requires knowledge of just x(t) and z(t), so I would like to define these functions by accessing elements of test.

1 件のコメント

which takes in the time parameter, t, and returns a 1x2 array of floats that depend on that value of t.
It does that under certain circumstances. In general it returns an array with the same size as t except in dimension 2, where the size is twice as large as t's size in dimension 2.
test = @(t) [t, t^2];
t = magic(4)
t = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
result = test(t)
result = 4×8
16 2 3 13 345 257 281 273 5 11 10 8 257 313 305 281 9 7 6 12 281 305 313 257 4 14 15 1 273 281 257 345
[size(t); size(result)]
ans = 2×2
4 4 4 8
So would you really want the second element of the output or the second half of the output?

サインインしてコメントする。

回答 (2 件)

VBBV
VBBV 2023 年 4 月 26 日
編集済み: VBBV 2023 年 4 月 26 日

0 投票

test = @(t) [t, t^2]
test = function_handle with value:
@(t)[t,t^2]
% test using t = 10
y = test(10);
% define e.g function
x = @(t) t.^2+2*t
x = function_handle with value:
@(t)t.^2+2*t
% get the value of function using first value returned by test
x(y(1))
ans = 120

2 件のコメント

Jasminder
Jasminder 2023 年 4 月 26 日
Hi VBBV, I think there is a misunderstanding on what I asked. I want x(t) to be defined as the first element of test. In Mathematica notation, this could be:
x = @(t) test(t)(1)
In this simple example, of course, this should give
x=@(t) t,
but the syntax test(t)(1) in invalid. How to get around this?
VBBV
VBBV 2023 年 4 月 26 日
編集済み: VBBV 2023 年 4 月 26 日
ok, i guess you need a cell array to build function as follows
x = @(t) t;
y = @(t) t^2;
% e.g function
test = {x , y}
test = 1×2 cell array
{@(t)t} {@(t)t^2}
% call or access the first element of e.g function
x = test(1)
x = 1×1 cell array
{@(t)t}
% do something next
you can also extend test function at your will

サインインしてコメントする。

chicken vector
chicken vector 2023 年 4 月 26 日

0 投票

The fact that you can't concatenate indexing is a well-known Matlab limitation whose implementation has already been suggested on the forum.
The best option that you have is to use:
x = @(t) t
By doing so you are also saving computational time because you want to compute test to only access the first element is a waste of resources.

2 件のコメント

Jasminder
Jasminder 2023 年 4 月 26 日
As the note in the OP suggests, I do not in fact know what the function x is. I am working with vectors and so I can determine (after some calculation) the array [x(t), y(t), z(t)]. The rest of the calculation then requires just x(t) and z(t). Therefore, this suggestion does not work. Do you know of an alternative approach?
chicken vector
chicken vector 2023 年 4 月 26 日
This might be too intricate for your needs but it works:
test = @(t) [t,t^2];
x = @(t) extractFirstTestValue(test,t);
function firstValue = extractFirstTestValue(test,t)
testValue = test(t);
firstValue = testValue(1);
end

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2023 年 4 月 26 日

コメント済み:

2023 年 4 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by