2016-12-01 82 views
0

我有一个16位/像素的图像文件。使用Matlab,我想生成另一个数组,其中每个元素只包含位2-10。我可以做一个for循环,但它太慢了:Bitshift在Matlab中没有循环的数组中的每个元素

if mod(j,2) ~= 0  
    image1(i,j) = bitshift(image(i,j),-2); % shift the LL bits to the left 
else    
    tmp = bitand(image(i,j),3);   % save off the lower 2 bits 00000011 
    adder = bitshift(tmp,6);    % shift to new positions in LL 
    image1(i,j) = bitshift(image(i,j),-2); % shift the UL bits to the right 
    image1(i,j-1) = bitor(image1(i,j-1),adder); add in the bits from the UL 
end 

有没有办法做类似以下的事情?

image1(:,1:2:end) = bitshift(image(:,1:2:end),-2); 
etc 
+0

您是否按照您发布的方式尝试了它? 'bitshift'和相关的函数都可以接受多维输入。 – Suever

+0

是的,错误是“下标分配维度不匹配”。 – user7236719

+1

您是否在尝试设置'image1 = image'之前确保'image1'的尺寸合适? – Suever

回答

0

bitshiftbitand,并bitor呼叫所有多维阵列作为第一输入操作。你遇到的问题可能是因为你已经初始化image1是一个不同的尺寸比image,因此你会得到一个尺寸不匹配错误使用以下命令

image1(:,1:2:end) = bitshift(image(:,1:2:end),-2); 

为了解决这个问题,初始化image1要么image或在调用上述命令之前的大小为image的零的阵列

image1 = zeros(size(image)); 
image1(:,1:2:end) = bitshift(image(:,1:2:end),-2); 
相关问题