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.
Walter Roberson
Walter Roberson 2017 年 3 月 20 日
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 ExchangeProgramming についてさらに検索

タグ

質問済み:

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