remove zero padding for binary vector and string

7 次查看(过去 30 天)
case 1
a=[1 0 0 1 0 1 1 1 1 1 0 0 1 0 1 0 0 1 0 0 0 0 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0];
d=[1 0 1 0 1 0 1 1 0 0 1 1 0 1 1 1 0 1 1 0 1 0 0 1 0 0 1 1 1 0 0 0 1 0 1 1 1 1 0 0 1 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 1 1 1 1 1]
q=zeros(1,length(d)-length(a))
w=[a q]
case 2
x = input('x: ','s')
y = input('y: ','s')
nx = length(x)
ny = length(y)
if ny > nx
sprintf(sprintf('%%0%is',ny),x)
elseif nx > ny
sprintf(sprintf('%%0%is',nx),y)
else
display('nx=ny')
end
how to remove zero padding for case 1 and case 2? can anyone help me?

回答(1 个)

Voss
Voss 2023-12-20
case 1
a=[1 0 0 1 0 1 1 1 1 1 0 0 1 0 1 0 0 1 0 0 0 0 0 1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0];
d=[1 0 1 0 1 0 1 1 0 0 1 1 0 1 1 1 0 1 1 0 1 0 0 1 0 0 1 1 1 0 0 0 1 0 1 1 1 1 0 0 1 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 1 1 1 1 1];
w=a;
case 2
Possibly this:
x = input('x: ','s')
y = input('y: ','s')
nx = length(x)
ny = length(y)
if ny > nx
sprintf('%*s',ny,x)
elseif nx > ny
sprintf('%*s',nx,y)
else
display('nx=ny')
end
Or possibly this:
x = input('x: ','s')
y = input('y: ','s')
nx = length(x)
ny = length(y)
if ny > nx
sprintf('%s',x)
elseif nx > ny
sprintf('%s',y)
else
display('nx=ny')
end

类别

Help CenterFile Exchange 中查找有关 Signal Generation, Analysis, and Preprocessing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by