How do I make the following a function?

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

Answers (3)

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 Comments

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?
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])

Sign in to comment.

[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 Comments

Hi,
Thank you for responding, when i tried this i got an error even when i created a new function file. Am i doing something wrong?
What error you got?
What error are you getting?
Note: the code will have problems if A is not scalar or vector.
function Christopher_Clarke_mean_and_standard_deviation
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
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])
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])

Sign in to comment.

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 Comments

>> 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.
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.

Sign in to comment.

Asked:

on 18 May 2020

Commented:

on 18 May 2020

Community Treasure Hunt

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

Start Hunting!