フィルターのクリア

Hump-day puzzler - Unknown Function

1 回表示 (過去 30 日間)
Matt Fig
Matt Fig 2011 年 5 月 25 日
My colleague was working with MATLAB and defined an anonymous function F. When he saw me come in he typed clc, hit return than challenged me to guess the form of the function only by calling it however many times I want. After calling it many times, I have figured out that it takes a scalar argument, and when the value of the argument is greater than 0 it does this:
>> F(1)
Name Size Bytes Class Attributes
---- 0;']) ------------------------------------------
x 1x1 8 double
And when the value of the argument is less than zero, it appears to do nothing:
>> F(-1)
>>
Can you help me figure out what the function looks like?
  2 件のコメント
Matt Fig
Matt Fig 2011 年 5 月 25 日
Hint #1. The function does not call an M-File
Matt Fig
Matt Fig 2011 年 5 月 25 日
Hint #2. Here's how I know the function wants a scalar argument...
>> S.one = 't';
>> F(S)
>> F({3})
>> F(magic(2))
>>

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

採用された回答

Laura Proctor
Laura Proctor 2011 年 5 月 25 日
I get the sense that this is not exactly what the user did, but this seems to do what is expected as a first try:
F=@(x) eval('if isscalar(x)&&x>0, whos x, end')
===========================
Second iterate: I needed to add in a check for type double. It also prints out the funky characters with WHOS (but without any functionality). :oP
F=@(x) eval(['if isscalar(x)&&isa(x,''double'')&&x>0, whos x, end; 1>0;'])
  5 件のコメント
Matt Fig
Matt Fig 2011 年 5 月 25 日
Do you know why WHOS prints this way when used like that? I.E., why it behaves differently than it does here:
F=@(x) eval(['if isscalar(x)&&isa(x,''double'')&&x<0, whos x, end; 1<0;'])
Matt Fig
Matt Fig 2011 年 5 月 25 日
@Oleg, you aren't the only one who doesn't understand why this happens. I was hoping Laura could shed some light. See my previous comment.

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

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2011 年 5 月 25 日
Tough one! In lieu of a real way to name a function/function handle: 0;'])
F = @(x)HDC525(x);
and:
function HDC525(x)
%SCd 5/25/2011: Lame attempt at HD challenger
if ~isscalar(x)||x<=0
return
else
Str = evalc('whos x');
fprintf('%s\n ---- 0;'']) ------------------------------------------\n %s',Str(1:numel(Str)/2-1),Str(numel(Str)/2+1:end));
end
  2 件のコメント
Matt Fig
Matt Fig 2011 年 5 月 25 日
See Hint #1. Though I think you know this. I will post Hint #2 in a little while. This is a tough one!
Sean de Wolski
Sean de Wolski 2011 年 5 月 25 日
I know it doesn't. And I know that 'whos' will put the function handle's meat where you have _0;'])_ but I know how to overwrite it. Arghhhhh.

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

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by