Clear Filters
Clear Filters

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

2 views (last 30 days)
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
James Tursa
James Tursa on 4 Nov 2016
Edited: James Tursa on 4 Nov 2016
"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

Image Analyst
Image Analyst on 4 Nov 2016
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
Mike Denny
Mike Denny on 7 Nov 2016
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.
Walter Roberson
Walter Roberson on 7 Nov 2016
(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
Mike Denny
Mike Denny on 4 Nov 2016
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?
Matt J
Matt J on 5 Nov 2016
Edited: Matt J on 5 Nov 2016
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 MATLAB 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!