0

How I can create a square matrix for a fixed dimension such that each element in this matrix is a function e.g., \begin{equation} A=\left(\begin{array}{ccc} x & \sin x & x^2\\ 1-x & e^x & 2x & \\ \cos x & x-2 & 3x\end{array}\right) \end{equation} so that I can use these elements in a loop?

dim=3;
A=zeros(dim,dim);
B=zeros(n,n,dim)
n=10;
x=2*(1:n);
h=1/n
for j=1:dim
    for i=1:n;
        B(i,i,j)=h+(here I want A(i,j))
    end;
end;

This is only a rough code just to clarify how I want to use the elements of $A$??

mzp
  • 1,925
  • 2
  • 18
  • 41
S.N.A
  • 199
  • This is a bit off topic for here, really, but I can probably help you anyway. Do you want these to be functions (e.g. @(x) x) or symbolic expressions? – Ian Apr 05 '18 at 19:21
  • Functions please.. – S.N.A Apr 05 '18 at 20:27
  • Then A will need to be a cell array instead of a matrix. You also unfortunately will not be able to do things quite the way you wrote in the snippet, but you can get close. – Ian Apr 06 '18 at 02:31
  • Thank you for the idea, can you show me a way to write @(x) to call it in a loop? – S.N.A Apr 06 '18 at 12:13
  • For example, A=cell(3); A{1,1}=@(x) x; A{1,2}=@(x) sin(x); etc. Then you can retrieve the elements the same way you assigned them, using curly braces. – Ian Apr 06 '18 at 16:15
  • Thank you Ian, I tried it, it works even though it makes the code more complicated.. – S.N.A Apr 06 '18 at 21:57
  • I'm not sure I understand your code snippet, however, would defining $A$ as an anonymous function help? In other words, A = @(x) [x,sin(x),x.^2;1-x,exp(x),2*x;cos(x),x-2,3*x]; – Kyle Apr 08 '18 at 03:59
  • @Kyle 's suggestion would also work, depending on how A is going to be used. – Ian Apr 10 '18 at 02:35

0 Answers0