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?

for j=1:dim
    for i=1:n;
        B(i,i,j)=h+(here I want A(i,j))

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

  • $\begingroup$ 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? $\endgroup$ – Ian Apr 5 '18 at 19:21
  • $\begingroup$ Functions please.. $\endgroup$ – S.N.A Apr 5 '18 at 20:27
  • $\begingroup$ 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. $\endgroup$ – Ian Apr 6 '18 at 2:31
  • $\begingroup$ Thank you for the idea, can you show me a way to write @(x) to call it in a loop? $\endgroup$ – S.N.A Apr 6 '18 at 12:13
  • $\begingroup$ 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. $\endgroup$ – Ian Apr 6 '18 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.