matlab - 翻转向量的最后 3 位

标签 matlab binary

我有一个 uint16 向量,我需要翻转每个数字的最后 3 位。

我已经这样做了,但我认为必须有更简单的解决方案来做到这一点。这是我的代码。

%Turn the vector to binary
V_bin = dec2bin(V,16);
for i=1:length(V)
  %Get the last 3 bits
  tmp = V_bin(14:end);
  %Convert the string to decimal
  tmpdec = bin2dec(tmp);
  %Do the flip
  tmpflip = bitcmp(uint8(tmpdec));
  %Flipped to binary
  tmpbin = dec2bin(tmpflip);
  %Replace the flipped bits in the original string
  V_bin(14:end) = tmpbin(6:end);
end
V = bin2dec(V_bin);

正如你所看到的,一个简单的操作有很多行,我想知道是否有更有效的方法来做同样的事情。

最佳答案

我不熟悉 matlab,但是 bitxor功能看起来适合你,即

V = bitxor(V, 7);

关于matlab - 翻转向量的最后 3 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15401322/

相关文章:

matlab - 找到向量中最长的连续整数

matlab - Matlab 中 IFFT 的缩放问题

matlab - 使用Matlab修改文本文件

objective-c - 获取最左边的位

你能直接用 c 语言家族对表示为二进制数字的值进行编码吗?

algorithm - 如何将表示为数字数组的数字从 base-2^k 转换为二进制?

matlab - Matlab 中向量的高斯滤波器

javascript - JavaScript 中的二进制到字符串

C++ cout 二进制值

matlab - 将函数 fminunc 与逻辑回归的 BFGS 方法进行比较