Functions Matlab, how to modify?

조회 수: 6 (최근 30일)
Jenny Andersen
Jenny Andersen 2019년 11월 24일
댓글: Jenny Andersen 2019년 11월 25일
Hi,
so I have two vectors, Q = (a,l,m) and Z = (d,b,c). I have created a function to calculate Z, but the problem is that I don't want the answear to be d= something1, b=something2 and c= something3. I want it to be Z = =[something1, something2, something3] when I call the function.
function [a,l,m]=test1(d,b,c)
a=0;
l=b/sqrt(b^2 + c^2);
m=c/sqrt(d^2 + c^2);
a=a*1;
a=a*-1;
l=l*1;
l=l*-1;
m=m*1;
m=m*-1;
Is it possbible to modify the function in some way to achive this?
  댓글 수: 2
Turlough Hughes
Turlough Hughes 2019년 11월 24일
You would write
function Z = test1(d,b,c)
% operations to get a,l,m.
Z=[a l m];
end
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 25일
편집: KALYAN ACHARJYA 2019년 11월 25일
function Z = test1(d,b,c)
a=0;
l=b/sqrt(b^2 + c^2);
m=c/sqrt(d^2 + c^2);
a=a*1;
a=a*-1;
l=l*1;
l=l*-1;
m=m*1;
m=m*-1;
Z=[a l m];
fprintf('Z=[%i %i %i]\n',Z);
end

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

채택된 답변

Turlough Hughes
Turlough Hughes 2019년 11월 25일
편집: Turlough Hughes 2019년 11월 25일
Hi Jenny,
You could write your function to have two outputs as follows:
function [Z1,Z2] = test1(d,b,c)
% code to determine a,l,m
Z1=[a,l,m];
Z2=[a,-l,-m];
end
When calling the function you have to specify two outputs if you want them otherwise matlab does not know what to assign the second output to. Basically:
[Z1,Z2]=test1(d,b,c)
Another thing you could do would be to write a single output where you have 6 elements in a single output variable. That would go something like this:
function Z = test1(d,b,c)
% code to get a,l,m
Z=[a,l,m;a,-l,-m];
end
When you call the function now you get a single output with six elements that you are looking for.
I recommend looking at onramp tutorials. Especially part 7 on calling functions as well as vectors and matrices, part 3. Though, if you intend to use matlab much you are as well to go through all of the sections.
  댓글 수: 1
Jenny Andersen
Jenny Andersen 2019년 11월 25일
Thank you alot! It seems to be working very well now. One thing that came to mind is that the value differs a bit from be previous expressions, but I gess that is the way functions work.
I will continue with the Onramp course.

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

추가 답변 (1개)

Jenny Andersen
Jenny Andersen 2019년 11월 25일
편집: Jenny Andersen 2019년 11월 25일
So I tried Turlough's suggestion again and it seems to be working fine now. But since two vectors will be generated (-d,b,c) and (d,b,-c) I would like to add this information to my code in some way. I have come up with the following, but still I only get one vector.
function [Z] = test1(d,b,c)
a1=0;
l1=b/sqrt(b^2 + c^2);
m1=-c/sqrt(d^2 + c^2);
a=0;
l=-b/sqrt(b^2 + c^2);
m=c/sqrt(d^2 + c^2);
Z=[a1 l1 m1];
Z=[a l m];
end
  댓글 수: 1
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 25일
편집: KALYAN ACHARJYA 2019년 11월 25일
Why you have deleted your previous comments, its may confuse to the answer providers? This is answer section, please post your comment in comment section only.

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by