Conversion of a binary matrix to decimal matrix
8 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I wonder if there is an easier way to convert a binary matrix to a decimal matrix?
For example: A = [1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0; 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0] (2x16 matrix) will be converted into B = [255 0; 15 240] (2x2 matrix). So, 8 bit binary numbers in the original matrix will be converted into decimal numbers. Or, in general, an Mx8N binary matrice will be converted into an MxN decimal matrice.
I know i can do it by using for loops and binaryVectorToDecimal or bi2de functions.
However, there might be a more easier way to do it by utilizing another built-in functions.
Thanks in advance,
0 个评论
采纳的回答
Stephen23
2022-3-14
编辑:Stephen23
2022-3-14
The most efficient approach would probably be to avoid type conversion (e.g. to character) and to avoid BIN2DEC.
It is just as easy to do the whole thing with basic numeric operations:
A = [1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0;0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0]
M = reshape(pow2(7:-1:0)*reshape(A.',8,[]),[],size(A,1)).'
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!