Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Created July 25, 2014 01:50
Show Gist options
  • Save tonyfast/14be381c478bf0de62ee to your computer and use it in GitHub Desktop.
Save tonyfast/14be381c478bf0de62ee to your computer and use it in GitHub Desktop.
Trim zeros and nan's off of an N-D array in matlab
function trimmed = trimnd( trimmed )
% Trim an N-D matrix with extra zero columns.
% Delete zero slices for a more compact viz
for ii = 1 : 3
b = all( trimmed == 0 | isnan( trimmed ) ,ii);
for jj = setdiff( 1 : 3, ii )
deletecol = setdiff( 1 : 3, [ii,jj] );
b2 = all( b, jj );
for qq = 1 : 3; evalindex{qq} = ':';end
evalindex{deletecol} = 'b2';
evalexp = sprintf( '%s(%s,%s,%s)= [];','trimmed', ...
evalindex{1}, evalindex{2}, evalindex{3} );
eval( evalexp );
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment