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

回答(3 个)

Walter Roberson
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 个评论
Christopher Clarke
Christopher Clarke 2020-5-18
i am earning as i go so i aplogies in advance if i ask an obvious question.
How do i call the function to accept any N numbers?
Walter Roberson
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
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 个评论
KSSV
KSSV 2020-5-18
编辑:KSSV 2020-5-18
A = [1 2 4 5 6] ;
[mu,sd] = myfunction(A) ;
I have mentioned you already how to call the function.
If you follwed what is hown in the image..use
Christopher_Clarke_mean_and_standard_deviation([1 2 4 5 6])
Walter Roberson
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
Christopher Clarke 2020-5-18
function Christopher_Clarke_mean_and_standard_deviation([1 2 4 5 6])
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
  2 个评论
Christopher Clarke
Christopher Clarke 2020-5-18
>> Christopher_Clarke_mean_and_standard_deviation
Error: File: Christopher_Clarke_mean_and_standard_deviation.m Line: 1 Column: 57
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Abouve is the error message i get.
Walter Roberson
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:
  1. Emptiness, as in the ([1 2 4 5 6]) not appearing at all, just function Christopher_Clarke_mean_and_standard_deviation
  2. () with nothing inside, as in function Christopher_Clarke_mean_and_standard_deviation()
  3. () 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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by