write a matrix from a function (say h(m,n), where m and n are running index)

Suppose I want to write a matrix from a function (h(m,n)), so in mathematica, It can be written in foollowing way " Table[h[m, n], {m, 1, 3}, {n,1, 3}];" this will wrtie a matrix of 3x3, how to do the same thing in matlab ??

Answers (1)

myFun = @(x,y) x.^2 + y;
result = bsxfun(myFun,1:3,(1:3).')
If I understood your question right, then you'd just need to replace myFun with your own function.

4 Comments

thank you! well this is working, but if I write "myFun = @(x,y) hermiteh(x,2)*hermiteh(y,3); result = bsxfun(myFun,1:3,(1:3).')"
It gives three-four errors, (e.g.-inner matrix dimesions must agree...)
PLease check this
Basically I Want to write the following function;
lagu=@(j,k,l) feval(symengine,'laguerreL',j,k,l);
ele1=@(j,k) exp(0.25)*sqrt(2.^k/factorial(k))*sqrt(factorial (j)/2.^j)*lagu(j,k-j,-0.5)*(0.5).^(k-j);
ele2=@(j,k) exp(0.25)*sqrt(2.^k/factorial(k))*sqrt(factorial (j)/2.^j)*lagu(j,k-j,-0.5)*(-0.5).^(k-j);
el=@(j,k) 0.5*i*(ele1(j,k)-ele2(j,k));
res=bsxfun(el,1:2,(1:2).')
when I run this, it gives so many errors, please help
el should produce a scalar for this approach to work. I don't have the symbolic math toolbox so I can't test your code.

Sign in to comment.

Asked:

on 30 Aug 2017

Commented:

on 30 Aug 2017

Community Treasure Hunt

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

Start Hunting!