How do I concatenate binary values from 32 cells into 1 cell?

5 次查看(过去 30 天)
I have a 32 bit binary value in an array of size 1x32. I want to turn this array into a 1x1 with a concatenation of all the bits. How may I go about doing so?
EDIT:
I was a little vague in my initial question. To give more context, I have an input array of size and type - 188583x1 double. The rows are filled with decimal numbers. I need to convert the decimal numbers to binary numbers which will later on be used for bit-wise operations. I have tried to use dec2bin() on the entire array but that leaves me with char type data. To my knowledge, I cannot perform bit-wise operations on char type data. I explored the option of using str2num() but that takes away all leading zeros (I need to maintain my 32 bits if I want to perform bit-wise operations). So currently I have written the following:
%Importing Data
[RW,MemAddr_d] = readvars('quick4k.txt');
MemAddr_b = de2bi(MemAddr_d,'left-msb');
% Attempt to convert decimal array to Binary Array
[rows,cols] = size(MemAddr_b);
padding = zeros(rows,(32-cols));
MemAddr_32b = horzcat(padding,MemAddr_b);
This gives me all 188583 rows but in 32 separate columns. I would like to concatenate the data in those columns to form a 188583x1 array. If there is a better way to go about doing this, please do let me know! Thanks!

回答(1 个)

Walter Roberson
Walter Roberson 2020-7-29
V = rand(1,32) < 0.3; %example data
As_Scalar = sum(V .* 2.^(31:-1:0));
disp(dec2bin(As_Scalar, 32)) %cross-check
  5 个评论
Oscar Rodrigues
Oscar Rodrigues 2020-7-30
dec2bin(YourArrayOfNumbers, 32) - '0'
Doesn't give me what I need though, since its in 32 separate columns. I need all the data to be 1 single column. Is there any way to do this?
Walter Roberson
Walter Roberson 2020-7-30
You want separate bits in order to be able to do bitwise operations, so you need to be able to index by row and by bit number. That calls for a 2D array unless you use cell arrays,
num2cell(dec2bin(YourArrayOfNumbers, 32) - '0',2)
In that limited sense, you would get a something-by-1 array (a column). But it would be a cell array, and you cannot do bitwise operations on a cell array: you would have to access the contents of the cell array using {} indexing to be able to access the bits.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by