MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by will on 4 Aug 2013

I need to write a function that computes the mean, standard deviation, minimum & maximum values and range of a ten element vector, without using the `std` function. Instead i've been given the equation (last x is meant to be x-bar but couldn't work out how to write it):

σ =√((1/N)(∑_(i=1)^n)▒(xᵢ-x) )

N is the number of data points (10 in this case) and x-bar is the mean value of x.

I can use any other functions except `std`.

*No products are associated with this question.*

Answer by Cedric Wannaz on 4 Aug 2013

Edited by Cedric Wannaz on 4 Aug 2013

Accepted answer

Say you have

>> x = [4, 8, 7, 5] ; >> b = 6 ;

If you wanted to subtract `b` to each element of `x`, you would do it with:

>> y = x - b y = -2 2 1 -1

this is a vector operation: in one shot, you operate on all elements, without having to use a loop. Now if you look up for element-wise operations in MATLAB, you will realize that for operators which have a matrix definition, like the product `*`, there is an element-wise version whose syntax involves a dot, e.g. `.*`. Same for `^` and `.^`.

You might not know what a matrix product is, but the element-wise product is the usual product applied to each element. Now I let you experiment with that, e.g. if you wanted to square all elements of a vector.

will on 4 Aug 2013

i understood most of what you said but i'm still unsure with what i need to do for the question? sorry to be a pain

Cedric Wannaz on 4 Aug 2013

You are not a pain, but, for obvious reasons, I cannot give you the answer. The last hint that I can give you is the following: usually in MATLAB we don't perform sums by looping over a range, summing/stacking terms, in the way the mathematical expression (with ∑) is suggesting. Instead, we compute a vector of all the terms of the sum, e.g.

>> x = [7, 5, 6] ; % Defined by previous computation.

note that these are numerical values, that we then sum:

>> theSum = sum(x) theSum = 18

Now you have all the information that you need.. just proceed by small steps: try to compute terms that have to be summed, sum them, perform other operations if needed.

## 1 Comment

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/83942#comment_170631

I have restored the original text of this question.

@will: this question has a clear subject and an accepted answer, so it may be valuable to someone else in the future. If you have a good reason why it should be removed from MATLAB Answers, please flag the question, explain why it should be deleted, and an administrator or high-reputation contributor will consider deleting the question. Please

do notsimply edit your question away.