how to define a function handle if i need to define a function from R^n to R?

4 views (last 30 days)
Hello! I'm sorry for my dumb question but i need to define a function f(x) from R^n to R where n is very large (say n=1000) but if I use the command f = @(x) = sum(1/2*x(i,:)^2+x(i,:)) it gives me error if I try to insert a x wich belongs to R^n and it only works if I put a scalar value. What's the correct sintex? How do I define this function?
  1 Comment
James Tursa
James Tursa on 16 Dec 2020
I am confused about what you want. Can you provide a short example of input and desires output? E.g., what would be the desired output for the following inputs:
x = reshape(1:24,2,3,4);
x = reshape(1:120,2,3,4,5);

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 16 Dec 2020
I am not certain what you are doing or what result you want.
Try these to see which one gives you what you want:
fr = @(x) sum(1/2*x(:).^2+x(:),2); % Use Element-Wise exponentiation (.^) & Force Column Vectors To Sum Across Columns
fc = @(x) sum(1/2*x(:).^2+x(:)); % Use Element-Wise exponentiation (.^) & Force Column Vectors To Sum Down Columns
x = rand(5,1);
yr = fr(rx);
yc = fc(x);
.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!