How do I make the following a function?
1 回表示 (過去 30 日間)
古いコメントを表示
A =rand(1,100);
% Christopher Clarke
% 10/05/2020
% This function calculates the mean and standard deviation without
% using in-built functions
% M is the mean
% VSD is the Standard Deviation
som=0;
for i=1:length(A)
som=som+A(i);
end
M=som/length(A);
% Above is the calculation for the mean
moy=0;
for i=1:length(A)
moy = (A(i)-M)^2;
moy = sqrt((moy)/length(A));
end
VSD=moy/length(A);
% Above is the calculation for the Standard deviation
0 件のコメント
回答 (3 件)
Walter Roberson
2020 年 5 月 18 日
Before any of that code, insert the line
function Christopher_Clarke_mean_and_standard_deviation
and store it in file Christopher_Clarke_mean_and_standard_deviation.m
2 件のコメント
Walter Roberson
2020 年 5 月 18 日
How do i call the function to accept any N numbers?
function Christopher_Clarke_mean_and_standard_deviation(A)
and pass in the numbers when you call the function
Christopher_Clarke_mean_and_standard_deviation([1, 2, 4, 5, 6])
KSSV
2020 年 5 月 18 日
[M,VSD] = myfunction(A) ;
% Christopher Clarke
% 10/05/2020
% This function calculates the mean and standard deviation without
% using in-built functions
% M is the mean
% VSD is the Standard Deviation
som=0;
for i=1:length(A)
som=som+A(i);
end
M=som/length(A);
% Above is the calculation for the mean
moy=0;
for i=1:length(A)
moy = (A(i)-M)^2;
moy = sqrt((moy)/length(A));
end
VSD=moy/length(A);
% Above is the calculation for the Standard deviation
Save the above function...
A = rand(1,100) ;
[mu,sd] = myfunction(A) ;
6 件のコメント
Walter Roberson
2020 年 5 月 18 日
function Christopher_Clarke_mean_and_standard_deviation(A)
A = rand(1,100);
% Christopher Clarke
% 10/05/2020
% This function calculates the mean and standard deviation without
% using in-built functions
% M is the mean
% VSD is the Standard Deviation
som = 0;
for i=1:length(A)
som = som + A(i);
end
M = som/length(A);
% Above is the calculation for the mean
moy = 0;
for i=1:length(A)
moy = (A(i)-M)^2;
moy = sqrt((moy)/length(A));
end
VSD = moy/length(A);
% Above is the calculation for the Standard deviation
This will accept a single argument, but will then promptly ignore it and over-write it with the random values.
You need to make a decision: will the A matrix be generated inside the function, or will it be something that you accept as a parameter?
Another note:
Christopher_Clarke_mean_and_standard_deviation [1,2,4,5,6]
In MATLAB, when you name a function as the first thing on a line, and then you have whitespace, and the next non-whitespace on the line is not open-bracket or comma or semi-colon, then MATLAB takes what is there as characters and passes in the characters to the command. The full rules about exactly when it stops taking characters is a bit complicated. Anyhow, the effect of the above would be that the character vector '[1,2,4,5,6]' would be passed in to the function. In order to have what you type interpreted as numeric, you need to use () around it, like
Christopher_Clarke_mean_and_standard_deviation([1,2,4,5,6])
Christopher Clarke
2020 年 5 月 18 日
2 件のコメント
Walter Roberson
2020 年 5 月 18 日
function Christopher_Clarke_mean_and_standard_deviation([1 2 4 5 6])
^^^^^^^^^^^^^
At the place I point out in a function line, you are permitted one of several things:
- Emptiness, as in the ([1 2 4 5 6]) not appearing at all, just function Christopher_Clarke_mean_and_standard_deviation
- () with nothing inside, as in function Christopher_Clarke_mean_and_standard_deviation()
- () with a list of items inside, separated by commas. Each item may be a plain unindexed variable name, or else the special indication ~ or the keyword varargin . For example |function Christopher_Clarke_mean_and_standard_deviation(A,~, varargin)
It is never valid to put a literal constant of any form inside the () on the function line, and it is never valid to put indexed variables there either.
The purpose of a function line is to establish a local name for whatever value is passed in by the user in the corresponding position. Putting something like [1 2 4 5 6] there does not establish any connection between a value passed in by the user and a local variable name.
It is also not possible to establish default values for input on the function line.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!