Byte Array / Bit Conversions in a Cell
42 views (last 30 days)
Show older comments
I'm not too familiar with Matlab, and I'm having a trouble with cell operations. I have 2 questions:
1- ) Think a byte array cell which consist of 1000x4 uint8 type. I want to convert it into a cell which is 1000x1 uint32 type. Byte order of byte array is big endian. I'm having a trouble with implementing the 'typecast' method to this cell without any loop for time efficiency.
2-) Again think the uint8 type cell which is 1000x1. I need to split it to bits. As a result I want to obtain 8 cells 1000x1 for every bit. But again I could not handle with the 'bitget' method for cells.
Thanks a lot!
0 Comments
Accepted Answer
Steven Lord
on 22 Jun 2021
If all your numeric data is the same type, I recommend storing it in a numeric array rather than as a cell array. Let's make some sample int8 data:
rng default
x = randi([0 intmax('int8')], 3, 4, 'int8')
Now to convert this to int32 we need to first make it a vector. I'm going to convert each row of x into an int32 so:
xvec = reshape(x.', 1, [])
y = typecast(xvec, 'int32').'
Let's look at the hex digit patterns.
format hex
disp(x)
disp(y)
Or perhaps you want the first column of x to have the most significant bits in y, the second column the next most significant, etc.? If so take a look at the swapbytes function.
disp(swapbytes(y))
More Answers (0)
See Also
Categories
Find more on Characters and Strings 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!