# removing zeros from matrix

5 views (last 30 days)
Perri Johnson on 19 Dec 2021
Commented: Perri Johnson on 20 Dec 2021
Hi,
I've constructed a code looking at vertical force data and have it set to put each stance phase into it's own column. The one issue I'm having is that I have all these extra zeros. Any way to remove them so I don't have to export all of these zeros with the data I care about? Have tried "remove," "nonzeros," and "~=0" functions but with no luck.
##### 1 CommentShowHide None
Jan on 19 Dec 2021
Post the code you have tried and explain "no luck" with details. Then the readers can mention, what the problem is.

Jan on 19 Dec 2021
x(x==0) = [];
% Or:
x = x(x~=0);
Where do the zeros come from? Instead of removing them it might be easier to avoid to create them before.
##### 1 CommentShowHide None
Perri Johnson on 20 Dec 2021
Up until line 91, t_step becomes a 2694x10 matrix (a lot of the cells are zeros) because each stance phase is seperated by where it occurs over the time duration. when I try, t_step = t_step(t_step~=0); t_step becomes a 1649x1 matrix. I'm trying to keep all of the non zero values so I don't have a huge number of cells with zeros when I export the data.
the zeros come from this for loop that I have set up from lines 66 - 87
for i = 2:length(time)
p = p+1;
if TO == 10 %once the last element is reached, code breaks out of the for loop to prevent error
break
end
if grf_z(i) >= threshold && grf_z(i-1) < threshold
HS=HS+1;
t_RON(HS) = time(i);
f_RON(HS) = grf_z(i);
q = q+1;
elseif grf_z(i) < threshold && grf_z(i-1) >= threshold
TO=TO+1;
t_ROFF(TO)=time(i-1);
f_ROFF(TO)=grf_z(i-1);
end
if grf_z(i)>=threshold
t_step(p,q)=time(i);
f_step(p,q)=grf_z(i);
end
end