MATLAB Answers

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

1 ビュー (過去 30 日間)
Angus Wong
Angus Wong 2019 年 4 月 29 日
編集済み: Angus Wong 2019 年 5 月 1 日
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 件のコメント
Angus Wong
Angus Wong 2019 年 4 月 29 日
x is a variable.
The script is something like this:
clear; close all; clear;
k=input('Input a number for x');
for a=1:k
This is a simplified version.


回答 (1 件)

Kevin Phung
Kevin Phung 2019 年 4 月 29 日
編集済み: Kevin Phung 2019 年 4 月 29 日
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))
  3 件のコメント
Angus Wong
Angus Wong 2019 年 4 月 29 日
But thanks a lot for your hard work!





Community Treasure Hunt

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

Start Hunting!

Translated by