从Matlab中的向量中删除前导零
Delete leading zeros from vector in Matlab
我可以 trim 向量中的前导零 [0 0 0 2 8 12] , [0 1 8 0 3 0] , [0 0 0 0 25 0] 关闭以获得 [2 8 12] , [1 8 0 3 0] , [25 0] 具有相同的功能?
有没有不使用 while
循环的方法? (或任何其他类型的循环?)
我打算将矢量转换为字符串,例如“2h 8m 12s”、“1mo 8d 0h 3m 0s”、“25m 0s”,如果这样可以打开任何门的话。
正如@Luis Mendo 提到的那样,find() 可以解决问题。
a=[0 0 0 2 8 12];
b=[0 1 8 0 3 0];
c=[0 0 0 0 25 0];
a_short = a(find(a>0,1):end);
b_short = b(find(b>0,1):end);
c_short = b(find(c>0,1):end);
find() 中的“>0”部分不是必需的,但我觉得它有助于提高可读性
我可以 trim 向量中的前导零 [0 0 0 2 8 12] , [0 1 8 0 3 0] , [0 0 0 0 25 0] 关闭以获得 [2 8 12] , [1 8 0 3 0] , [25 0] 具有相同的功能?
有没有不使用 while
循环的方法? (或任何其他类型的循环?)
我打算将矢量转换为字符串,例如“2h 8m 12s”、“1mo 8d 0h 3m 0s”、“25m 0s”,如果这样可以打开任何门的话。
正如@Luis Mendo 提到的那样,find() 可以解决问题。
a=[0 0 0 2 8 12];
b=[0 1 8 0 3 0];
c=[0 0 0 0 25 0];
a_short = a(find(a>0,1):end);
b_short = b(find(b>0,1):end);
c_short = b(find(c>0,1):end);
find() 中的“>0”部分不是必需的,但我觉得它有助于提高可读性