hi, how to store two different matrices as an output parameter of a 'function'.
for example: function[matrix1, matrix2]= input(in1,in2) so matrix1 and matrix2 contain some m*n matrix which i obtain through my logic. how do i put these 2 matrices in my function output ? becoz when i use the above function format, output displays only 1 matrix, ie, in this case matrix1 alone.
thank you.

 채택된 답변

Walter Roberson
Walter Roberson 2011년 11월 21일

3 개 추천

When you invoke the function, you need to provide two variables on output:
[m1, m2] = input(in1, in2);
Note: naming a routine "input" will almost certainly lead to later problems, as "input" is the name of a MATLAB routine.
EDIT: complete example:
>> type rakeshtest
function [matrix1,matrix2] = rakeshtest(in1, in2);
matrix1 = ones(size(in1));
matrix2 = zeros(size(in2));
end
>> [m1,m2] = rakeshtest(rand(3,5),rand(2,4))
m1 =
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
m2 =
0 0 0 0
0 0 0 0

댓글 수: 11

Rakesh Praveen
Rakesh Praveen 2011년 11월 21일
oh i am sorry to mention it as input. Actually my function name is different & i didn't name it as input. BTW thanks for d note. yes, i have given two variables on output [m1, m2].but only m1 is being displayed and not m2. tats my query. where m1 contains 3×8 matrix & m2 contains 1×8 matrix.
Walter Roberson
Walter Roberson 2011년 11월 21일
See edit with complete example.
bym
bym 2011년 11월 21일
what do you get when you do
whos m1 m2
Rakesh Praveen
Rakesh Praveen 2011년 11월 22일
@ WALTER i got your point now. it was my mistake, i was expecting output in a wrong way. thank you very much for your example.
RB
RB 2017년 3월 20일
편집: Walter Roberson 2017년 3월 20일
@Walter
Can I use the above function handle in the ode function? Actually I am solving a system of matrix odes where dx/dt, dy/dt, dz/dt are all 2 by 2 matrices and I need to get X,Y,Z which are again 2 by 2 matrices. My code is
function [dXdt, dYdt, dVdt] = mwish7(t, X, Y, V, A, B, R)
X = reshape(X, size(A));
Y = reshape(Y, size(A));
V = reshape(V, size(A));
dXdt = -A.'*X - X*A + 2*X*(B.'*B)*X - R;
dYdt = -(A-(B.'*B)*X).'*Y;
dVdt = -Y.'*(B.'*B)*Y;
dXdt = dXdt(:);
dYdt = dYdt(:);
dVdt = dVdt(:);
end
and the ode45 program I use is
A=[-0.5, 0.4; 0.007, -0.008];
B=[0.06 -0.0006; -0.06, 0.006];
R = [1 0; 0 1];
X0 = [0, 0; 0, 0];
Y0 = [1 0; 0 1];
V0 = [0, 0; 0, 0];
[T X Y V] = ode45(@(t,X,Y,V)mwish7(t, X, Y, V, A, B, R), [15 0], X0, Y0, V0)
I get the error: NOT ENOUGH INPUT ARGUMENTS.
Thanks.
RB:
You are not restricted in what you can call within your ode calculation function.
Your ode calculation function must return only a single column vector.
It is permitted to have your ode function accept a column vector for its second parameter, which it can then reshape() as needed for its purposes, and you may extract values from that. For example,
X0 = rand(2,2);
Y0 = randn(2,2);
Z0 = [1 3; 9 -3];
initial_value = [X0, Y0, Z0];
ode45(@YourFunction, tspan, initial_value(:))
...
function dy = YourFunction(t, xyz)
xyz = reshape(xyz, 2, 6);
X = xyz(:,1:2); Y = xyz(:,3:4), Z = xyz(:,5:6);
...
RB
RB 2017년 3월 20일
편집: Walter Roberson 2017년 3월 20일
Thanks for the help. I used the above suggestion and it worked. I used
dy = [-A.'*X - X*A + 2*X*(B.'*B)*X - R, -(A-(B.'*B)*X).'*Y, -Y.'*(B.'*B)*Y];
dy = dy(:);
Can you suggest any method to print the output as a 2 by 12 matrix OR X Y and Z separately?
Thanks and regards,
RB
Walter Roberson
Walter Roberson 2017년 3월 20일
편집: Walter Roberson 2017년 3월 20일
You could modify your line
[T X Y V] = ode45(@(t,X,Y,V)mwish7(t, X, Y, V, A, B, R), [15 0], X0, Y0, V0)
to
initial_value = [X0, Y0, V0];
[T XYV] = ode45(@(t,X,Y,V)mwish7(t, XYV, A, B, R), [15 0], initial_value(:));
XYV = reshape(XYV, 2, 2, 3, []));
X = permute( XYV(:,:,1,:), [1 2 4 3]);
Y = permute( XYV(:,:,2,:), [1 2 4 3]);
V = permute( XYV(:,:,3,:), [1 2 4 3]);
This would give 2 x 2 x N arrays, where N = length(T) -- the number of time points returned. This would not be 2: when you specify a tspan of length 2 then ode45 will generate intermediate points and return them. If you specified a tspan of length 3 or more, then ode45 would return the data only for the particular times you specified.
RB
RB 2017년 3월 20일
Thanks again. When I try this I get the following error:
Error using reshape To RESHAPE the number of elements must not change.
Error in mNcwish7 (line 2) xyz = reshape(xyz, 2, 6); %Convert from "12^2"-by-1 to "2"-by-"6"
Error in mYNCne7>@(t,x,y,z)mNcwish7(t,xyz,A,B,R)
Error in odearguments (line 87) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in mYNCne7 (line 9) [T xyz] = ode45(@(t,x,y,z)mNcwish7(t, xyz, A, B, R), [15 0], initial_value(:));
Thanks and regrds, RB
Walter Roberson
Walter Roberson 2017년 3월 20일
See attached.
RB
RB 2017년 3월 20일
Thanks so much. It works perfectly.
Regards, RB

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2011년 11월 21일

댓글:

RB
2017년 3월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by