Why do "size" and "max" dimension conventions contradict?

I've found a "DIM" convention within matlab that is contradictory and very tricky to find if you're not looking for it. Seriously, this seems like one of those convention mistakes that causes space probes to crash into things.
If you type in
help size
You'll get some text that says "M = size(X,DIM) returns the length of the dimension specified by the scalar DIM. For example, size(X,1) returns the number of rows. If DIM > NDIMS(X), M will be 1."
Ok, so a DIM of 1 means rows, 2 means columns then, got it.
Then if you look at
help max
You get this
[Y,I] = max(X,[],DIM) operates along the dimension DIM.
Example: If X = [2 8 4 then max(X,[],1) is [7 8 9],
7 3 9]
max(X,[],2) is [8
9]
Whoa! Hold on, now the DIM value of 1 means columns all of a sudden?! What the hell is this?! This seems literally irresponsible of MathWorks to do this. I expect programming syntax to be consistent.
Maybe I'm finally "joining the club" of people who realize this. And I'm guessing there's nothing anyone can do about it since, if they switch now, all previous scripts where the people realized this contradiction would produce errors after the correction.
So that in mind I guess my main point is make this somehow obvious, like by explicitly stating this deviation in the text of the help commands. If I'm reading the help quickly and see, "Oh, ok, it's variable name, then square brackets, then DIM." I'm also not going to think "huh, I should investigate this passive example to see if DIM has the same convention or not." In this situation, I just happened to stumble on it by mistake.
Are there also times when a "+" actually subtracts? Please let me know if there are!

1 Comment

"Are there also times when a "+" actually subtracts? Please let me know if there are!"
Yes. One could write a class that overloaded the + operator to do subtraction. :)

Sign in to comment.

 Accepted Answer

I don't see an inconsistency either, and I never did. Basically 1 means the first index which means "rows". So whether you do size(m, 1) to count going down the rows, or max(m, [], 1) to find the max going down the rows, either way you're processing by going down the rows of the matrix. I see no problem with it, and I'm not aware of being in any club about it.

10 Comments

Same convention for other functions as well. E.g., the sum function:
B = sum(A,dim)
Maybe that means we are members of the dim sum club.
either way you're processing by going down the rows of the matrix
I think you mean "going down the columns". Rows are horizontal, so you would go across them, but not down them. :D
But the idea of 1 means rows, and you travel down the rows, makes a lot of sense and matches well with the "size" function. You count the number of rows as you proceed through them. Likewise if I want to find the max value in each row, we should proceed down through the rows one by one and find the max value in each.
Image Analyst, did you think one thing and type another or did you just realize you are also confused? ;)
And with regard to the sum function, ok that matches up with the max function. From the help: if A is a matrix, then sum(A,2) is a column vector containing the sum of each row.
Then ok, dimension 2 is what you use to analyze each row (but not count the number of rows, that's a column property).
HOWEVER, then we just displace the issue. If dimension 2 deals with each of the rows and dim 1 with each of the columns, then why is the location convention not column-by-row? If I want to find the value of a position in the matrix, I write X(row,column), not X(column,row). So if the location convention is X(DIM2,DIM1) then logic would have it that a 3 dimensional matrix location would be X(DIM2,DIM1,DIM0) and then I have to go negative I have have even more dimensions. So now how does this logic play out if I have max(X,[],3)? Or should I say max(X,[],0)?
Ummm ... I doubt Image Analyst is confused at all on this ...
But the idea of 1 means rows, and you travel down the rows, makes a lot of sense and matches well with the "size" function. You count the number of rows as you proceed through them. Likewise if I want to find the max value in each row, we should proceed down through the rows one by one and find the max value in each.
I think I understand now. But no, "counting rows" is not the only valid intuition for the meaning of size(x,dim). The meaning of size(x,1) is "how many allowable row indices are there?" The meaning of max(x,[],1) is "what is the maximum value I hit by varying the row index in x(row,col)?". In both cases, we are examining what happens when the row index is varied. That is the intuition that MATLAB follows, and quite consistently.
I'm not confused, and I believe I described it accurately. Let's say you have 3 rows and 100 columns - a very short and wide matrix. Now, talking about dimension #1, for each column, as you go down (or "along") the rows within that column you'll encounter row 1 then row 2 then row 3 and finally get one result for that one column. Since there are 100 columns, there will be 100 results (counting, summing, getting the max, or whatever). You get one result for each column. Indeed, if you typed out the original matrix and then the results, which will be a row vector, underneath it you'll see the 100 results line up nicely with the 100 columns of the original 2-D matrix. Just as you'd expect.
Now, let's say you operate on dimension #2. So for each row, you go across/within/along that row from column 1 to column 2 to column 3 to .... to column 100, doing whatever operation you want. It's like you're collapsing the array "sideways". So you'll get 3 results: one for each row. The result is a column vector (not a row vector) and perhaps that's where you're getting confused. If you were to type the result on the left side of the original matrix, you'd see the 3 elements lines up nicely in a 3x1 column vector to the left of the rows. Which makes sense because you operated horizontally, i.e. within rows but across columns.
You would not want the result of operating sideways along/across/within/whatever-you-want-to-call-it 3 rows to be a row vector with 1 row and 3 columns when the original had 100 columns. You want operations vertically to give row vectors and operations horizontally to give column vectors.
Makes perfect sense to me. I believe it's internally consistent across functions. However that's not so with the (row,column) vs. (x,y) paradigm, where both are used but the order is different (vertical coordinate first vs. vertical coordinate last). That's a watchout that trips up countless new users and sometimes even experts.
row, column vs x, y still trips me up sometimes.
Though functions hard-coded for meshgrid instead of ndgrid trip me up more often. Which is really just another form of row/column vs x, y.
Ok Image Analyst, that description cleared it up for me. It was really helpful to picture the resulting vectors placed next to the original matrix, and that the functions cause the matrix to collapse one direction or another.
Now to keep them straight in my head. I guess I'll just picture that a 1 is like a column and the matrix collapses along the axis of the 1.
At least I know that the functions are consistent now, that mostly alleviates my frustration. There's still just a little left over that (row, column) vs (x, y) convention, but one thing isn't so much to keep track of.
(row, column) notation is fundamentally at odds with (x, y) notation. It isn't just MATLAB.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 4 Nov 2016
Edited: Matt J on 4 Nov 2016
There is no inconsistency. max(x,[],1) gives you the maximum value in every column x(:,i) while size(x,1) gives you the length of every column x(:,i).
The confusion, I guess, is in the fact that "the number of rows" and "the length of the columns" are different phrasings of the same thing.

2 Comments

But if that's the case then the location convention is screwy as I discuss below Image Analyst's answer. How do you think that fits in logically with the DIM convention?
Because a column is what you get from x(i,j) when you fix j and visit different i along the DIM=1 axis.

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!