Why do "size" and "max" dimension conventions contradict?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mike Denny
el 4 de Nov. de 2016
Comentada: Walter Roberson
el 7 de Nov. de 2016
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 comentario
James Tursa
el 4 de Nov. de 2016
Editada: James Tursa
el 4 de Nov. de 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. :)
Respuesta aceptada
Image Analyst
el 4 de Nov. de 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 comentarios
Walter Roberson
el 7 de Nov. de 2016
(row, column) notation is fundamentally at odds with (x, y) notation. It isn't just MATLAB.
Más respuestas (1)
Matt J
el 4 de Nov. de 2016
Editada: Matt J
el 4 de Nov. de 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 comentarios
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!