This is a problem I seem to come up against quite often in Matlab and have never really found a good solution to. Maybe there isn't one since it is a matrix-based language in essence.
As we all know, the lowest dimensionality for a variable in Matlab is 2d, meaning that what we would call a vector in other languages has to be explicitlty a row (1,n) or a column (n,1).
I do a lot of signal processing and frankly I don't care if my data is a row or a column, I just want to manipulate it as a sequence and move it along to the next part of the program.
The problem is that my sequences are created in various places, from various sources and so inevitably they sometimes end up as a column and sometimes as a row. And I have never found a neat way of dealing with this.
For example, my current problem is that I have a sequence and I need to pad it to a specific length. Conceptually this is simple - I work out the difference in length, half it and put zeros of that number at the start and end. But whilst I know my sequence is 1d (i.e. isvector returns true) I don't know at this point, for sure if it is a column or a row. I can create my zeros for padding using size( mySequence ) and replacing the non-singleton with the padding size, though that is still messy, but then I have to concatenate anyway and I know no way of simply telling Matlab 'concatenate these 1d sequences in the obvious way'. I have to do either horzcat or vertcat, meaning an ugly if statement, or I can use cat( dim, ... ) but I still have to acquire the index of the non-singleton dimension.
So am I missing something? Does someone have a nice neat way to solve the following problem
"Given a 1d sequence, pad the sequence symmetrically with 0s (adding excess at the end if required) and return it in whatever orientation it was originally"?
This is the 'best' I have come up with, but it's not exactly neat!
mySequence = rand( 12,1 );
targetLength = 25;
sequenceLength = numel( mySequence );
nonSingletonDim = find( size( mySequence) ~= 1, 1 );
padding = ( targetLength - sequenceLength ) / 2;
prePadSize = size( mySequence );
prePadSize( nonSingletonDim ) = floor( padding );
postPadSize = prePadSize;
postPadSize( nonSingletonDim ) = ceil( padding );
result = cat( nonSingletonDim, zeros( prePadSize ), mySequence, zeros( postPadSize ) );