Created
July 25, 2014 01:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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