How to obtain an array of constant values from vector valued anonymous functions

24 Ansichten (letzte 30 Tage)
Hello Everyone,
I have an anonymous function whose result is a constant number; when I call it passing a vector variable as input I obtain a scalar constant instead of a vector of constants. See the following example
>> f=@(x) 3.5; v=0:10; y=f(v)
y =
3.5000
I wonder if there is a simple solution allowing a vector with eleven components (3.5's) in y to be obtained.
I know it's an unusual need, but the associated output variable is one of many others based on similar (non-constant) functions, whose results have to be plotted, so the size of their outputs must be correct. Of course, I'd have several possibilities to overcome this without anonymous functions, but it would be not very elegant. When I realized it, I thought it may be a bug, though a very minor one.
Thank you in advance, I hope someone can help.

Akzeptierte Antwort

the cyclist
the cyclist am 10 Feb. 2021
f=@(x) 3.5 * ones(size(x));
v=0:10;
y=f(v)
y = 1×11
3.5000 3.5000 3.5000 3.5000 3.5000 3.5000 3.5000 3.5000 3.5000 3.5000 3.5000

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by