In Matlab, does a pad command exist, which also pads in the negative dimension
(therefore removing indices in a specified dimension)?

[Edit: I knew something did this - fft does this for the input, but it also takes the fft first.]

I have a function which can receive input data x in n_dim dimensions.

I would like to remove all but n datapoints from a user specified dimension dim.
I could use shiftdim to always make the specified dimension the first dimension;
however, how do I code such that I do not need a finite number of colons
to represent the dimensions of the input data x?

x   = rand(01,12,01); % n_dim = 1  
y   = rand(04,12,01); % n_dim = 2  
z   = rand(04,12,07); % n_dim = 3

n   = 3


dim = 1
y = y(1:n, :    );
z = z(1:n, :, : ); % Note that extra colons are needed depending on n_dim.


dim = 2

x = x(   1:n    );
y = y(:, 1:n,   );
z = z(:, 1:n, : ); % Note that extra colons are needed depending on n_dim.

Do I need to use the shiftdim command to place dim in the first dimension,
and then place the 1:n within the eval command
along with a string variable which contains as many ,:s as needed?

  • eval does not work with these comma's and semicolons, because it really evaluates the statement in the string. Similiar to if you tell matlab 1:n,:,:, it will just throw an error.
    – BramMooij
    Oct 21 '16 at 18:45
  • For the shifting of dimensions I would indeed use shiftdim. You can use it like this OutputMatrix = shiftdim(InputMatrix,dim-1); Then apply your operations on OutputMatrix: OutputMatrix = shiftdim(OutputMatrix,n_dims-(dim-1));
    – BramMooij
    Oct 21 '16 at 18:52
  • @BramMooij : Yeah, but I could create a for loop or something similar which adds as many ,: as necessary to the eval string.
    – kando
    Oct 23 '16 at 17:56
  • Did you find a solution?
    – BramMooij
    Nov 10 '16 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.