# How to turn a 1x32 array into a 1x1 array?

18 views (last 30 days)
Oscar Rodrigues on 30 Jul 2020
Answered: Walter Roberson on 30 Jul 2020
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-shifting and logical and-ing.
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!
Image Analyst on 30 Jul 2020
You forgot to attach the text file. So what do you want to do to the 32 columns to produce a single column? The mean of all the values? The max, min, median? What???
Oscar Rodrigues on 30 Jul 2020
There are 32 bits spaced out into 32 columns. I would like to manipulate the data in the array to achieve 32 bits in 1 column.

Rub Ron on 30 Jul 2020
Perhpas this? Say v is your 188583x32 matrix:
v = magic(5);
v_final = join(sprintfc('%d',v),'');
Oscar Rodrigues on 30 Jul 2020
Thank you so much!

Walter Roberson on 30 Jul 2020
You want to break up your decimal numbers into bits. I already advised you that you can do bit operations on decimal numbers, but your asking this follow-up question demonstrates that that is not acceptable to you, that you want to break up the numbers into bits. In order to be able to access a particular broken-out bit for a particular row, you need two indices: row number and bit number. That would be a 2D array, not a single column. The closest you could get would be to go for a cell array that was itself a column vector, in which case you would have to use {} indexing to pull out one particular row, and then index by bit number to get a particular bit. You cannot do vectorized manipulation of bits if you were to do this.
v_final = join(sprintfc('%d',v),'');
That is not acceptable for your purposes. You already specifically said that dec2bin() gives you character vectors and that that was not acceptable for your purposes because you cannot do bit manipulation on characters. sprintfc() used in that particular way is going to give you a cell array of character vectors that you cannot do bit manipulations on because of the cell array level and because of the character level.
If you needed a column you would be better to use
string( dec2bin(YourArray, 32) )
which would give you a column, but it would be a column of string objects, and you cannot do bit manipulation on string objects.
I have the feeling that you would even consider
sscanf(strjoin(cellstr(dec2bin(v)),newline),'%d')
to get a column that would look something like
10001
10111
100
1010
1011
11000
101
110
which would be a column in which you can "see" the bits. However, this would fail for two reasons:
1. These are decimal numbers, and you cannot do the kind of bit manipulation you were looking for on such numbers. Accessing individual bits from these decimal numbers requires using mod() and division and floor().
2. You have 32 bits, and a 32 bit decimal number would overflow maximum precision, even when each digit is either 0 or 1. You would only be able to store 20 such digits as decimal numbers before running into precision problems.
If you do not need streams of individual bits (2D array to access individual bits!!), as I discussed with you earlier, then leave the numbers in their original form and use bitget() / bitset() . Only switch to the individual-bit representation using techniques such as dec2bin(), for presentation purposes or because you need individual bits in ways that are only compatible with 2D indexing (or one continuous bit vector.) Reconstructing numbers or character strings or string objects out of the bits just for the sake of having one column, is a contradiction of your purposes.