Anonymous function arguments and parameters extrction

2 ビュー (過去 30 日間)
Ken
Ken 2022 年 4 月 1 日
回答済み: Jan 2022 年 4 月 1 日
If I have the anonymous function:
pu = @(x, y) [x,y];
is it npossible to extract the value of the argument x after the function has acted on it?
(Apologies if this does not make sense)

回答 (2 件)

Steven Lord
Steven Lord 2022 年 4 月 1 日
As written, as long as you know how long either one of the inputs was, yes.
P = @(x, y) [x, y];
x1 = 1:5;
y1 = 6:10;
z = P(x1, y1)
z = 1×10
1 2 3 4 5 6 7 8 9 10
x2 = z(1:5); % I know how long x was, extract that many elements from z
isequal(x2, x1)
ans = logical
1
x3 = z;
x3(end-4:end) = []; % I know how long y was, remove that many elements from z
isequal(x3, x1)
ans = logical
1

Jan
Jan 2022 年 4 月 1 日
pu = @(x, y) [x,y];
a = pu(1, 2:3)
a = 1×3
1 2 3
b = pu(1:2, 3)
b = 1×3
1 2 3
isequal(a, b)
ans = logical
1
This means: No, you cannot decide, what the inputs have been based on the output.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by