matrices in function output ?

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.

 Risposta accettata

Walter Roberson
Walter Roberson il 21 Nov 2011
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 Commenti

Rakesh Praveen
Rakesh Praveen il 21 Nov 2011
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.
See edit with complete example.
bym
bym il 21 Nov 2011
what do you get when you do
whos m1 m2
Rakesh Praveen
Rakesh Praveen il 22 Nov 2011
@ 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 il 20 Mar 2017
Modificato: Walter Roberson il 20 Mar 2017
@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 il 20 Mar 2017
Modificato: Walter Roberson il 20 Mar 2017
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 il 20 Mar 2017
Modificato: Walter Roberson il 20 Mar 2017
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 il 20 Mar 2017
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
See attached.
RB
RB il 20 Mar 2017
Thanks so much. It works perfectly.
Regards, RB

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by