I am having problems in anonymous function (y=@(x)(...))

Let me re-word my querry.
Let's say there is a square matrix of v=[1 2 3 4 5 6 78 9 10; 11 12 13 14 15 16 17 18 19 20;21 22 23 24 25 26 27 28 29 30;31 32 33 34 35 36 37 38 39 40;41 42 43 44 45........ 99 100] and a is a variable that prompt the user to input. It is easy to obtain a square matrix of size a by a of the original matrix v (left hand corner) with the use of w=v(1:a,1:a), of course a is less than length(v).
However, I would like to know how to do the same for anonymous functions. v in this case will be a column matrix a quantities of "@(x)(B.*[x x^2 x^3 ......]);", where [x x^2 x^3 ......] is a row matrix with a elements.
Here is an example:
Let's say v={@(x)([x x^2 x^3 x^4]; @(x)([x^2 x^3 x^4 x^5]); @(x)([x^3 x^4 x^5 x^6]); @(x)([x^4 x^5 x^6 x^7])};
if a = 3, the new v would be v={@(x)([x x^2 x^3]; @(x)([x^2 x^3 x^4]); @(x)([x^3 x^4 x^5])};

댓글 수: 4

are you missing sum()'s?
Oh, there supposed to be sum() there, my copy and paste skills should improve. Let me change that. But the program still doesnt work.
ok, just wanted to make sure. Also, what is x supposed to be? I think maybe it'll be easier to construct the matrix if x was an input to your function fun, instead of creating so many anonymous functions.
Angus Wong
Angus Wong 2019년 4월 29일
편집: Angus Wong 2019년 4월 29일
x is a variable.
The script is something like this:
clear; close all; clear;
dat=load(data);
leadingcoe=coef(dat);
yo=fun(leadingcoe);
k=input('Input a number for x');
for a=1:k
disp(yo{a}(k));
end
This is a simplified version.

댓글을 달려면 로그인하십시오.

답변 (1개)

Kevin Phung
Kevin Phung 2019년 4월 29일
편집: Kevin Phung 2019년 4월 29일

0 개 추천

let me know if this is what youre trying to do with your anon functions:
function y = fun(a,x)
y = sum(triu(repmat(flip(x.^(1:a)),numel(a),1)),2)
%uncomment this if you want to see how the matrix looks like before summing the rows
%y = triu(repmat(flip(x.^(1:a)),numel(a),1))
end

댓글 수: 3

Emmm. Sorry. No, this is not I am looking for. Let's say a=[1 2 3 4 5];
I am expecting the output to be:
y={@(x)(sum(a.*[x x^2 x^3 x^4 x^5]));
@(x)(sum(a.*[0 x x^2 x^3 x^4]));
@(x)(sum(a.*[0 0 x x^2 x^3]));
@(x)(sum(a.*[0 0 0 x x^2]));
@(x)(sum(a.*[0 0 0 0 x]))};
After this, in the main script, I then define a numerical value to the variable x (let's say 6) and display the result. I expect the final output from the main script to be [44790;7464;1242;204;30].
44790 comes from 1*6+2*6^2+3*6^3+4^6^4+5*6^5.
I then am able to change the value assigned to x and get a new vector.
Hence, my x is not defined as a numerical value inside the anonymous function, but a variable.
Basically my intended program steps are: import the data set, determine the number of columns using size(,2), running the anonymous function (fun) with the leading coefficients as the sole input and output a set of anonymous functions based on that, able to set a numerical value for the variable x and output the results for the anonymous functions based on the vzalue of x as a column vector.
This function is intended to output an anonymous function.
But thanks a lot for your hard work!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2019년 4월 29일

편집:

2019년 5월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by