matrices in function output ?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
2011년 11월 21일
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
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
2011년 11월 21일
See edit with complete example.
bym
2011년 11월 21일
what do you get when you do
whos m1 m2
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
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
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
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
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
2017년 3월 20일
See attached.
RB
2017년 3월 20일
Thanks so much. It works perfectly.
Regards, RB
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
