C=bitand(A,B);返回A和B的按位与
bitshift(a,b);
其中a是被移位数,b是移的位数,a只能是正数,b为正数时表示向左移位,b是负数时表示向右移位
a=zeros(250,400);
a=uint8(a);
for i=1:1:250
for j=1:1:50
binstring = dec2bin(d(i,j),8);
m=str2num(binstring(:))';
a(i,1+(j-1)*8)=m(8);
a(i,2+(j-1)*8)=m(7);
a(i,3+(j-1)*8)=m(6);
a(i,4+(j-1)*8)=m(5);
a(i,5+(j-1)*8)=m(4);
a(i,6+(j-1)*8)=m(3);
a(i,7+(j-1)*8)=m(2);
a(i,8+(j-1)*8)=m(1);
end
end
其中dec2bin的作用是把d(i,j)由uint8类型转换为8个位宽的char类型
str2num的作用是将上述字符串转化为double类型存储在m中用于后续对a矩阵的填充
上述代码中有2个for循环,matlab对for循环的运行速度非常慢
将其写入for1.m中,在代码中可以直接调用:for1
for i=1:1:250
for j=1:1:50
%binstring = dec2bin(d(i,j),8);
% m=str2num(binstring(:))';
a(i,1+(j-1)*8)=bitand(bitshift(d(I,j),-0),1);
a(i,2+(j-1)*8)=bitand(bitshift(d(I,j),-1),1);
a(i,3+(j-1)*8)=bitand(bitshift(d(I,j),-2),1);
a(i,4+(j-1)*8)=bitand(bitshift(d(I,j),-3),1);
a(i,5+(j-1)*8)=bitand(bitshift(d(I,j),-4),1);
a(i,6+(j-1)*8)=bitand(bitshift(d(I,j),-5),1);
a(i,7+(j-1)*8)=bitand(bitshift(d(I,j),-6),1);
a(i,8+(j-1)*8)=bitand(bitshift(d(I,j),-7),1);
end
end
% for i=1:1:250
% for j=1:1:50
% binstring = dec2bin(d(i,j),8);
% m=str2num(binstring(:))';
a(1:1:250,1+((1:1:50)-1)*8)=bitand(bitshift(d(1:1:250,(1:1:50)),-0),1);
a(1:1:250,2+((1:1:50)-1)*8)=bitand(bitshift(d(1:1:250,(1:1:50)),-1),1);
a(1:1:250,3+((1:1:50)-1)*8)=bitand(bitshift(d(1:1:250,(1:1:50)),-2),1);
a(1:1:250,4+((1:1:50)-1)*8)=bitand(bitshift(d(1:1:250,(1:1:50)),-3),1);
a(1:1:250,5+((1:1:50)-1)*8)=bitand(bitshift(d(1:1:250,(1:1:50)),-4),1);
a(1:1:250,6+((1:1:50)-1)*8)=bitand(bitshift(d(1:1:250,(1:1:50)),-5),1);
a(1:1:250,7+((1:1:50)-1)*8)=bitand(bitshift(d(1:1:250,(1:1:50)),-6),1);
a(1:1:250,8+((1:1:50)-1)*8)=bitand(bitshift(d(1:1:250,(1:1:50)),-7),1);
% aa(i,(1:8)+(j-1)*8)=m(9-(1:8));
% end
% end