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.

조회 수: 28 (최근 30일)
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.
  댓글 수: 1
Adam
Adam 2018년 3월 15일
편집: Adam 2018년 3월 15일
This is something that is not all that intuitive, or rather, to me the logic is not sufficient that it sticks in my head what ' along dimension dim' means so I always just try it one command line and if I get it the wrong way I do the other one! I could try to just memorise it, but things where there are two options I often have trouble memorising exactly which it is when the logic is not sufficient to me to work it out without having to memorise.
Although the sentence pointed out by Steven Lord, that I hadn't noticed previously, does give a clear logic to remember it by, having said all that!

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2018년 3월 15일
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.
  댓글 수: 2
David Abidoye
David Abidoye 2018년 3월 15일
Now I understand. I just had a hunch 1 had to do with rows and 2 with columns.
Just verify, I want to know if i have perfect understanding... For a sum on a 2 by 3 matrix, since the first dimension(row) is nonsingleton, it squashes the matrix to have 1 row and gives a row vector as the result.
Wow... Now I think know why sum of a matrix gives a row vector...I just accepted it as it was but now I know why... Thanks.
Steven Lord
Steven Lord 2018년 3월 15일
If you call sum with just one input argument, MATLAB reduces the size in the first non-singleton dimension of that input to 1.
For your matrix of size [2 3], the first non-singleton dimension is dimension 1 so the result will be of size [1 3]. That is indeed a row vector.
If you had an array of size [1 2 3], the first non-singleton dimension is the second (the first is a singleton dimension) and the result would be of size [1 1 3]. If you had asked sum to sum that array over dimension 1 instead, the result would have been of size [1 2 3] (in this case there's no reduction to be done.)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Pawel Jastrzebski
Pawel Jastrzebski 2018년 3월 15일
편집: Pawel Jastrzebski 2018년 3월 15일
I think you've explained it yourself. It's how Matlab works (opposite to your expectation unfortunately).
  • dimension = 1 - carries out the computation along the columns.
  • dimension = 2 - carries out the computation along the rows.
  • dimension = 3 - carries out the computation along the depth (in 3D matrices).
Also see here:

카테고리

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