Trouble Understanding Slicing

I will leave the other questions aside, but I have to say that the absence of proper vectors in Matlab is a huge annoyance. It’s causing me endless trouble almost every time I use Matlab (which is at least 5 days a week.) You really should learn to appreciate it.

The absence of vectors in Matlab means that in all code you have to make a decision and try to keep track of whether vectors are row or column vectors. Matlab pretends to be column-major, and functions like sum, prod, max, plot, etc. etc. etc. all operate along columns. But almost all built-in Matlab functions return row-vectors(!!!), linspace, 1:n, mynewvariable(n) = 5, etc. etc. etc.

One of the very worst: Matlab’s for loops can iterate over row vectors, but not over column vectors :exploding_head: :rage:

Every time I write code I have to either check if an input is row or column, or enforce one or the other with x(:) or x(:).', and then I suddenly have to interface with code that prefers the opposite of what I have chosen (using row-vectors instead of column.)

Working in python or julia where there are proper 1-dimensional vectors is always a huge relief. It’s one of the worst things about Matlab.

(Yesterday, I was working with a class where most of the fields had column vectors, but one of them, for some ungodly reason, was a row vector. I thought, “let me use squeeze in my code, so I don’t have to special-case this field, to fix this”. Didn’t work, because squeeze removes dimensions of length 1 – except for row vectors which remain unchanged … :tired_face: In other words, squeezing a 1x3x4 array gives a 3x4, array, but a 1x3 array remains 1x3.)

(Postscript: Sorry about the heated post @kw_martin , but this is one of the things about Matlab that actually makes me genuinely angry just thinking about it.)

7 Likes