no input, two output function
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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 件のコメント
madhan ravi
2018 年 11 月 5 日
[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
Walter Roberson
2018 年 11 月 5 日
[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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
