Borrar filtros
Borrar filtros

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

5 visualizaciones (últimos 30 días)
Oscar Rodrigues
Oscar Rodrigues el 29 de Jul. de 2020
Comentada: Walter Roberson el 30 de Jul. de 2020
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!

Respuestas (1)

Walter Roberson
Walter Roberson el 29 de Jul. de 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 comentarios
Oscar Rodrigues
Oscar Rodrigues el 30 de Jul. de 2020
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 el 30 de Jul. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by