How do I concatenate binary values from 32 cells into 1 cell?
4 views (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
Walter Roberson
on 29 Jul 2020
V = rand(1,32) < 0.3; %example data
As_Scalar = sum(V .* 2.^(31:-1:0));
disp(dec2bin(As_Scalar, 32)) %cross-check
5 Comments
Walter Roberson
on 30 Jul 2020
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.
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!