程序功能:将数组中的非0元素依次移动数组的前端
程序代码:
R = randint(1,8,[0 3])
for p = 1:length(R)
if R(p) ~= 0 & p > 1
a = R(p);
R(p) = [];
R = [a R]
end
end
结果展示:
R =
3 1 2 3 3 2 0 1
R =
1 3 2 3 3 2 0 1
R =
2 1 3 3 3 2 0 1
R =
3 2 1 3 3 2 0 1
R =
3 3 2 1 3 2 0 1
R =
2 3 3 2 1 3 0 1
R =
1 2 3 3 2 1 3 0