Matrix as an output of a function

AI Thread Summary
The discussion revolves around using MATLAB to extract specific elements from a matrix generated by a function and integrating those elements. The user initially struggles with indexing the output of a function directly and learns that they must first assign the output to a variable before accessing specific elements. They then face challenges in integrating a matrix element over a range due to the variable's dependency on E. The conversation highlights the need for clarity in defining variables and functions to perform symbolic integration correctly. Ultimately, the user seeks a solution for integrating a specific matrix element while maintaining its dependency on E.
phynewb
Messages
13
Reaction score
0
Hi guys

I am new to matlab. I have a question in how to pick up matrix elements as a output of a function.

Code:
function result=test(E)
result=[1,2,3;4,5,6]*E;

For each E, test(E) will generate a 2x3 matrix.
I wonder how to pick up the matrix element,say test(2)(1,3).
When I type test(2)(1,3), it shows
? Error: ()-indexing must appear last in an index expression.
So how can I pick up the matrix element?
Thank you.
 
Physics news on Phys.org
Code:
x = test(1);
x(1,3)
 
Thank you a-tom-ic.
So I need other space to save the matrix first.
 
But here comes the other question.
I need to integrate test(E)(1,3): int(test(E)(1,3),E1,E2).
If I set x=test(E), then int(x,E1,E2) will be problematic as shown "Undefined function or variable 'E'.
Is any way to solve this problem? Thank you for help.
 
HI Phynewb,

i'm sorry i don't understand what your trying to do! Supply more information on your variables and the task. What is E? The int-Function is symbolic toolkit isn't it?
Code:
syms E;
int(E)
int(E,1,2)
 
Thanks a-tom-ic.

Say, f1(E),f2(E),f3(E) are three functions. Here E is real number and the outputs are nxn real matrices.
Define F(E)=f1(E)*f2(E)*f3(E). So E is also an number and output is a matrix.
The purpose is to calculate the integration of F(E)(a,b) over E from E0 to E1. Here F(E)(a,b) means the (a,b) matrix element of F(E) and it is a number:
int(F(E)(a,b),E0,E1).

As you said, I cannot pick up the (a,b) element by F(E)(a,b). I have to save it as other variable, say G. Let G=F(E) and then take G(a,b).
But once you save it as other variable, how can I do the integration? Because G has no E-dependence.
int(G(a,b),E0,E1) is problematic!
I wonder if anyway to solve the problem.
Hopefully I explain it clearly.
 

Similar threads

Back
Top