MATLAB Answers

matrices in function output ?

815 views (last 30 days)
Rakesh Praveen
Rakesh Praveen on 21 Nov 2011
Commented: RB on 20 Mar 2017
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.

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 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 Comments

Show 8 older comments
RB
RB on 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
RB
RB on 20 Mar 2017
Thanks so much. It works perfectly.
Regards, RB

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!