Matlab/Octave: how to write n-dimensional zero padding algorithm without eval -


i write "syntactical sugar" octave or matlab zero-padding function, user sends n-dimensional object , vector of <= n entries. vector contains new, equal or larger dimensions object, , object zero-padded match these dimensions. dimensions not specified left alone. 1 expected use is, given example 5d block x of 3d medical image volumes, can call

y = simplepad(x, [128 128 128]); 

and pad first 3 dimensions power of 2 wavelet analysis (in fact use separate function nextpwr2 find these dimensions) while leaving others.

i have racked brains on how write method avoiding dreaded eval, cannot far find way. can suggest solution? here more or less have:

function y = simplepad(x, pad) szx = size(x); n_pad = numel(pad); szy = [pad szx(n_pad+1:end)]; y = zeros(szy); indices_string = '('; n = 1:numel(szx)     indices_string = [indices_string, '1:', num2str(szx(n))];     if n < numel(szx)         indices_string = [indices_string, ','];     else         indices_string = [indices_string, ')'];     end end command = ['y',indices_string,'=x;']; eval(command); end 

as understand, want pass dynamic arguments function. can converting these arguments cell , call function passing cell content. so, function like:

function y = simplepad(x, pad)     szx = size(x);     n_pad = numel(pad);     szy = [pad szx(n_pad+1:end)];     y = x;     szyc = num2cell(szy);     y(szyc{:}) = 0; % warning: assume x array grows end 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -