MATLAB: Good day. Please can someone explain dimension in simple terms and how it applies to arrays. Also, what do singleton and non-singleton dimension mean and do they affect some builtin functions such as sum.

arraysumvar

For example sum(M,1) sums over columns and sum(M,2) sums over rows. I am confused because I expect sum(M,1) to be for rows and vice versa because arrays are written in row by column.

Best Answer

Two key sentences from the description of the dim input argument in the documentation for the sum function are:
"Dimension dim indicates the dimension whose length reduces to 1. The size(S,dim) is 1, while the sizes of all other dimensions remain the same."
So if you want to squash your matrix down to have 1 row, you sum in dimension 1 which takes the sum of each column of data. If you want to squash your matrix to have 1 column, you sum in dimension 2 which takes the sum of each row.