Say I have a variable:
X = [NaN, NaN, 2, 5, 1, 6, NaN, 1, NaN NaN];
I need to remove/trim NaNs only when they occur at the edges, either at the beginning or the end, so X would be:
X = [2, 5, 1, 6, NaN, 1];
where the NaN occurring at the middle was untouched. I wrote some codes that work, but I think the codes could be much simpler (I tend to overcomplicate my codes ?). Please suggest a simpler way to do this. Thanks!
NaNGroups = regionprops(isnan(X), 'Area').Area;if isnan(X(1)) X(1:NaNGroups(1)) = [];endif isnan(X(end)) X(end-NaNGroups(end)+1:end) = [];end
Best Answer