no input, two output function
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
The first part of this assignment is to create a function with no inputs and two outputs. The 15x1 matrices are X and Y so I've got
function [X,Y] = myfunct()
X=[1;2;3 etc]
Y=[1;2;3 etc]
plot(X,Y,'.b')
end
If I run the function, the only thing that appears in the workspace is ans. Is this because they are local variables? How do I assign them to memory? Because the next part of the assignment is to create a function with a "for" loop that adds or multiplies elements of the arrays. The inputs are X and Y, but since they aren't defined I think it's screwing up my code. I believe we are supposed to load the first function so the next function (two input two output) can grab the X and Y from memory, perform the operations and output m and b (this is for a linear regression). (fyi-We're not supposed to use the 'sum' function, gotta use loops)
eta: And how do I create a single column vector with 1 to n elements? Rows is easy but I couldn't figure out a column.
채택된 답변
madhan ravi
2018년 11월 5일
[X,Y] = myfunct % try calling it with two outputs
function [X,Y] = myfunct
X=[1;2;3];
Y=[1;2;3];
plot(X,Y,'.b')
end
댓글 수: 10
[X,Y] = myfunct %function calling at the beginning
[sumx,sumy] = myregret(X,Y)
G = X.'*X
H = X.'*Y
I = vpa(sumx.*sumy,4)
function [X,Y] = myfunct()
X=[1;2;3;4;5;6;7;8;9;10;11;12;13;14;15]
Y=[50;49;46;45.5;46;45;42.9;41.5;39;35.6;31;27.5;23;16.3;10]
plot(X,Y,'.b')
end
function [sumx,sumy] = myregret(X,Y)
n=length(X);
sumx = 0 ;
sumy = 0;
for i=2:n
sumx(i) = sumx(i-1) + X(i);
sumy(i) = sumy(i-1) + Y(i);
end
end
madhan ravi
2018년 11월 5일
myfunct returns the value of X and Y which is passed onto the next function as argument to calculate sums and sumy
[X,Y] = myfunct
Calls the function myfunct. Internally no matter what that function calls its variables, the first positional output from the function will be assigned to X in the workspace that is calling the function, and the second positional output will be assigned to Y. As far as the caller of myfunct is concerned, it would be perfectly fine if myfunct were defined by
function [SeaMonkey, unobtanium] = myfunct
unobtanium = randi(10, 1, 25) ;
SeaMonkey = sqrt(unobtanium + 7);
Because there is no connection between the names of the variables used in the function and the variables assigned to in the caller, since it is all positional.
madhan ravi
2018년 11월 5일
Thank you sir Walter
"sorry if I sound stupid but by "Calls" you mean 'asks the computer' and not 'gives the name'?"
"Calling" a function is basically when you tell MATLAB to run a function, by typing its name with the required inputs and outputs. Functions can be called:
- by typing it in the command window and then pressing enter, or
- by writing it in a script/function/... and then running that script/function/....
When you call MATLAB's own functions it is exactly the same, e.g. in the command window:
>> y = sin(0) % call function SIN with one input and one output.
y = 0
You do the same with your function (with the required input/output arguments):
>> [A,B] = myfunct()
As Walter Roberson already noted, the names used inside the function are irrelevant to what names you use for the input/output arguments when the function is called. Do we know the names that SIN uses internally in its calculations? No, we don't. The internal names are irrelevant. The same applies to your function when you call it.
How to call functions is explained in the introductory tutorials:
Adam
2018년 11월 5일
It just tells myfunct to return to output arguments and assign them to variables called A and B in the calling workspace.
A function is just like a black box with a bunch of things going in and a bunch of things coming out. There may be none going in or none going out and of those that are returned by the function (as given by your function definition line) you can choose to ignore as many as you wish, either by just not referring to them if they are at the end or by replacing them with ~ if they are earlier in the list and you want a later output e.g.
[X,Y] = myfunct( ); % Return 2 arguments, pass in 0 input arguments
X = myfunct( ); % Return only the first output argument and ignore any subsequent ones
[~,Y] = myfunct( ); % Ignore the first output but return the second (and ignore any that may come after the second).
madhan ravi
2018년 11월 11일
편집: madhan ravi
2018년 11월 11일
getData??
regressConst change it to regressConst1
Walter Roberson
2018년 11월 11일
Note: when you have a function defined in a script file, the name of the script file cannot be the same as the name of the function.
furcifer
2018년 11월 11일
Hi All, thanks for the help. I went through my code and finally got it to do what I wanted. It seems I was calling for the previous function outside the new function and that was screwing it up. Lesson learned, I hope. :)
madhan ravi
2018년 11월 12일
You can show your thanks to everyone by accepting the answer
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Whos에 대해 자세히 알아보기
참고 항목
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)
