首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Matlab中对数组中的每个元素进行位移位,而不是循环

在Matlab中对数组中的每个元素进行位移位,而不是循环
EN

Stack Overflow用户
提问于 2016-12-01 22:41:57
回答 1查看 232关注 0票数 0

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

代码语言:javascript
复制
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

有没有办法做下面这样的事情呢?

代码语言:javascript
复制
 image1(:,1:2:end) = bitshift(image(:,1:2:end),-2); 
 etc
EN

回答 1

Stack Overflow用户

发布于 2016-12-02 02:16:00

bitandbitor都将多维数组作为第一个输入进行操作。您看到的问题可能是因为您已将image1初始化为与image不同的大小,因此使用以下命令会出现尺寸不匹配错误

代码语言:javascript
复制
 image1(:,1:2:end) = bitshift(image(:,1:2:end),-2); 

要解决此问题,请在调用上述命令之前将image1初始化为imageimage大小的零数组

代码语言:javascript
复制
image1 = zeros(size(image));
image1(:,1:2:end) = bitshift(image(:,1:2:end),-2); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40913440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档