square matrix with elements as functions to call in a loop

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$??

 채택된 답변

Steven Lord
Steven Lord 2018년 4월 7일
Make A a cell array containing function handles.
A = {@sin, @(x) x.^2};
A{2}(1:10)
Or make A a function handle that returns a cell array.
A = @(x) {sin(x), x.^2};
B = A(1:10);
B{2}

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2018년 4월 7일

댓글:

2018년 4월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by