フィルターのクリア

Applying a constant function on a vector

5 ビュー (過去 30 日間)
Jose
Jose 2012 年 11 月 22 日
Hi,
I am writing a program for school. I need the user to input a function as a string, and then I need to process it.
What I am doing:
f = vectorize(inline(user_input_string));
x = linspace(0, 20, 20);
y = f(x);
...
This works perfectly except when the user inputs a constant function (such as f = 1). In order for my project to work, y must be a vector. But if the user inputs a constant function, Matlab automatically sets y to a sclalar, instead of a vector.
What can I do?

回答 (5 件)

Star Strider
Star Strider 2012 年 11 月 22 日
編集済み: Star Strider 2012 年 11 月 22 日
I'm not certain I completely understand your problem. However, you could test for a scalar and if something like f=1 was the input, perhaps set y to:
f = '1';
y = polyval(str2num(f), x);
If the test for a scalar was true, you could also consider something like:
user_input_string = sprintf('polyval(%s, x)', f);
Without knowing more, that's the best solution I can come up with.
  2 件のコメント
Jose
Jose 2012 年 11 月 22 日
No, that won't solve it. I'll explain better:
- the user inputs a function
- i apply the function on a vector (linspace(0, 20, 100))
- then i want to get a vector i can use later on a internal product
- however, if the function e constant (let's say, f = 1), if i apply the function on the vector, i don't get another vector, i get a scalar.
Hope it's is clearer now.
Star Strider
Star Strider 2012 年 11 月 22 日
If the user inputs a constant, what output for user_input_string do you want?
The polyval function outputs a vector of constant values equal to the scalar input value with a length equal to the length of your x-vector. It's the only option I can think of that's compatible with your user_input_string variable.
The version I posted earlier assumes the scalar is read in as a string. If you read f in as a numeric value instead, replace the %s with %f:
user_input_string = sprintf('polyval(%f, x)', f);

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


Walter Roberson
Walter Roberson 2012 年 11 月 22 日
if length(y) == 1; y = y(ones(size(x))); end

Matt J
Matt J 2012 年 11 月 22 日
編集済み: Matt J 2012 年 11 月 22 日
f = inline(user_input_string);
fh=@(x) f(x);
x = linspace(0, 20, 20);
y = arrayfun(fh,x);

Jose
Jose 2012 年 11 月 23 日
Thanks guys!

Matt Fig
Matt Fig 2012 年 11 月 23 日
You can specify the variable in your call to INLINE. For example, this works even if the user enters 2:
f = vectorize(inline(input('Enter a func of x : ','s'),'x'));
  2 件のコメント
Matt J
Matt J 2012 年 11 月 23 日
編集済み: Matt J 2012 年 11 月 23 日
No, it doesn't work around the issue cited by the OP. The code still gives
>> f(1:10)
ans =
1
whereas the desired output is ones(1,10).
Matt Fig
Matt Fig 2012 年 11 月 23 日
Ah, good catch...
I guess we could get fancy.
S = input('Enter a func of x : ','s');
f = vectorize(inline([S,'+zeros(size(x),class(x))'],'x'));
But, yuck.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by