3

I have a matrix in MATLAB, lets say:

a = [
  89  79  96  
  72  51  74  
  94  88  87  
  69  47  78
]

I want to subtract from each element the average of its column and divide by the column's standard deviation. How can I do it in a way which could be implemented to any other matrix without using loops.

thanks

1

3 Answers 3

5

If your version supports bsxfun (which is probably the case unless you have very old matlab version), you should use it, it's much faster than repmat, and consumes much less memory. You can just do: result = bsxfun(@rdivide,bsxfun(@minus,a,mean(a)),std(a))

1

You can use repmat to make your average/std vector the same size as your original matrix, then use direct computation like so:

[rows, cols] = size(a); %#to get the number of rows

avgc= repmat(avg(a),[rows 1]); %# average by column, vertically replicated by number of rows
stdc= repmat(std(a),[rows 1]); %# std by column, vertically replicated by number of rows
%# Here, a, avgc and stdc are the same size
result= (a - avgc) ./ stdc;

Edit:

Judging from a mathworks blog post,bsxfun solution is faster and consumes less memory (see acai answer). For moderate size matrices, I personally prefer repmat that makes code easier to read and debug (for me).

6
  • 1
    I would disagree that use of bsxfun is harder to read or debug! In fact, either usage is best preceded by a comment, describing what you are doing. Comments take no time to execute. They cost little to write, compared to the time they save in debugging, and when you need to change your code next year, or when you need to modify the code inherited from the fellow who was run over by the crosstown bus last week.
    – user85109
    Oct 16, 2011 at 10:03
  • @woodchips hence the 'I personally prefer'. This is subjective, I edited my answer to stress this a little more.
    – Laurent'
    Oct 16, 2011 at 10:08
  • 1
    My point is, in any case, use comments to make the code readable, in which case there is no valid reason (beyond pure inertia) to use the inefficient older style. Learning to use the better forms as a habit will improve your code.
    – user85109
    Oct 16, 2011 at 10:44
  • With repmat, it is clear which dimension is the singleton that is being "broadcasted"(using numpy term). I can only think of this one way that is more readable than bsxfun, but as @woodchips said, you can always use comment. Oct 16, 2011 at 17:01
  • but for ppl new to matlab, repmat forces you to think about which dimensions you want to expand, so it is a better exercise comparing to bsxfun Oct 16, 2011 at 17:06
1

You could also use the ZSCORE function from the Statistics Toolbox:

result = zscore(a)

In fact, it calls BSXFUN underneath, but it is careful not to divide by a zero standard deviation (you can look at the source code yourself: edit zscore)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.