Combine matrix elements into a hexadecimal number on Matlab - matlab

For example I have a matrix command = ['01';'03';'0B';'00';'00';'02';'C6';'2F']. I want to combine its elements in a hexadecimal number 01030C000002C62F. I have tried a lot of different methods but haven't figured it out.

So, this is a char array:
A = ['01';'03';'0B';'00';'00';'02';'C6';'2F'];
Convert it to cell, then to string:
B = strjoin(cellstr(A))
01030B000002C62F
Now convert from hexadecimal to decimal:
hex2dec(B)
ans =
7.2914e+16

Related

Apply MATLAB sscanf to convert from hex to dec for multiple elements in a column vector

In MATLAB, I'm trying to convert hex (2’s complement) to decimal for a column vector using the sscanf function. While I have successfully converted using the hex2dec function, however I am unsuccessful converting multiple elements (hex values) in a column vector with sscanf. Yet when I have only one element (hex value) in the column vector the hex to dec conversion is successful. How do you convert a column vector with multiple elements (hex values), using sscanf function, to hexadecimal, 2’s complement?
colx = ['FEEE'; 'FFFF'; 'FDDD']
conversion = typecast(uint16(sscanf(colx, '%x')), 'int16')
My output is:
conversion = -1
However the output should be:
conversion = -274
-1
-547
sscanf will linearize the char array into a vector before attempting the conversion.
You need to add some spaces, otherwise it will simply try to convert a big value. Also, the char array should be transposed since Matlab stores arrays in column major order. In your example, what sscanf actually tries to convert is the string FFFEFDEFDEFD which overflows into the maximum value represented by an uint16 (0xFFFF), which itself is the signed integer representation of -1.
Try:
colx2 = [colx' ; repmat(' ',1,size(colx,1))]
conversion = typecast(uint16(sscanf(colx2, '%x')), 'int16')
The result is:
conversion =
-274
-1
-547

Char array to numeric array in matlab

I have the following matrix A of size 3x2:
A = [12; 34; 56];
But the data is stored as chars. I want to convert it to the numeric array. str2num doesn't. Is there another method to do that?
Well, your array doesn't look like a 3-by-2 array. In any case, you are looking for a casting function:
A = double(A);
should convert your chars in double.
If I understand correctly, you have
A = ['12'; '34'; '56']; %// strings
and want to get
B = [1 2; 3 4; 5 6]; %// numbers
This could be done as follows: convert A to double to produce each character's ASCII code, and then subtract the code of character '0' to obtain the desired numbers. In fact, conversion to double is done implicitly when you subtract chars, so you can just use
B = A-'0';

Binary to DNA encoding

I have an 8 bit binary sequence. I need to encode this 8 bit binary sequence into DNA sequence.
E.g., I have 10011100, the encoding rule I'm following is,
A=00;T=11;G=10;C=01,
So I want it to be something like GCTA. Therefore I need 4 bit DNA sequence as result.
I need to do this for a 256 * 256 matrix where each element is an 8 bit binary sequence.
I've created the matrix using the following code
a=imread('C:\Users\Desktop\lena.png');
disp(a);
imshow(a);
for i=1:1:256
for j=1:1:256
b{i,j,1} = dec2bin(a(i,j),8);
end
end
disp(b)
Here's a no for loop approach for you. We can actually do this in three lines.
You have the first step which is to take each 8-bit number in your image and convert it into its binary representation. Take note that this is a 2D cell array that is the same size as the image you used for doing this conversion. Each cell array would be the representation of the number as a string.
Now, all you really need to do now is create a lookup, then use this lookup to generate four characters per location in a new 2D cell array. As such, I would use the containers.Map() class to create a key-value lookup where each pair of bits gets mapped to a single character. Once we do this, we can then use cellfun and iterate over each 8 character string in your cell array, break up the bits into 2 element strings, and use these as keys into our lookup. We will inevitably get 4 separate cells for the output, so we'll need to use cell2mat to bring it all back together. As such, try doing this:
codebook = containers.Map({'00','11','10','01'},{'A','T','G','C'}); %// Lookup
outputCell = cellfun(#(x) values(codebook, {x(1:2),x(3:4),x(5:6),x(7:8)}), ...
b, 'uni', 0);
finalOutput = cellfun(#cell2mat, outputCell, 'uni', 0);
As an example, let's say we had this 2 x 2 matrix of cell elements:
b = {'11111111', '10101010'; '11001100', '00001101'}
b =
'11111111' '10101010'
'11001100' '00001101'
Running through the above code, this is what we get:
finalOutput =
'TTTT' 'GGGG'
'TATA' 'AATC'
Similar to rayryeng's solution using a lookup table, but imho containers.Map() is overkill:
codebook = 'ACGT';
output = cellfun(#(x) codebook(bin2dec(reshape(x, 2, 4)') + 1), b, 'UniformOutput', false)
I don't think it gets much shorter if the input consists of "binary numbers" in the sense of 8-character 0/1-strings. reshape breaks the strings into 4 portions of 2 characters each, bin2dec transforms these into four numbers in the range 0 to 3, codebook(... + 1) translates these into the characters ACGT.
If the input consists of actual 8-bit binary numbers, e.g. the uint8 data a that you get from reading in that Lena image, you can save the detour through 0/1-strings and use base 4 from the start:
output = reshape(cellstr(codebook(dec2base(a, 4) - '0' + 1)), size(a))
Here dec2base(a, 4) represents the binary numbers as 4-character strings of characters '0' to '3', - '0' is a trick to get numbers 0 to 3, then the lookup as before, and finally some stuff to get everything in the cell-array-of-strings format.

reading int16 data in matlab

I read this set of hex values from an accelerometer datalogging:
35AC,2889,1899,0C4A,058B,FD46,F620,F001,EE44,EF08,EF46,F750,007F,0814,1369,21F3,34F0,45CE,5992,6D05,7C12,7FEF,7FF8,7FF8,7FF8,7FF8,7FD9,7F27,74A7,67D8,5826,468F,3621,2573,1326,0441,F88F,F1BF,F082,EADB,EAEE,EE04,F190,F89E,01F5,0B0C,155A,2721,3A20,48DC,5985,676A,721E,7C20,7FF8,7FEE,7F1B,
It should somehow draw a sinusoide curve, but I could not find the right import method for signed int16 and the curve jumps from 0 to 65535.
Could you please help me?
I have tried sscanf(...,'%4x')
The sscanf format for a signed hex int16 is just '%4i'. Unfortunately it expects that hex values will begin with 0x, which yours clearly don't. One possibility is for you to scan it in as you are, and then manually convert it to a signed int16. One possible way of doing this would be the following:
input = sscanf(...,'%4x');
input16 = typecast(uint16(input),'int16');
The values are being read in as a uint32 and automatically converted to double by the sscanf function. So we convert it to a uint16, then typecast it to a 16-bit int (note that just using int16(input) doesn't work, as it doesn't convert values over INT16_MAX to negative values).
string = '35AC,2889,1899,0C4A,058B,FD46,F620,F001,EE44,EF08,EF46,F750,007F,0814,1369,21F3,34F0,45CE,5992,6D05,7C12,7FEF,7FF8,7FF8,7FF8,7FF8,7FD9,7F27,74A7,67D8,5826,468F,3621,2573,1326,0441,F88F,F1BF,F082,EADB,EAEE,EE04,F190,F89E,01F5,0B0C,155A,2721,3A20,48DC,5985,676A,721E,7C20,7FF8,7FEE,7F1B';
%// Your data as a string
string = [string ',']; %// add ending comma to reshape into groups of five chars
strings = reshape(string,5,[]).';
strings = strings(:,1:4); %'// each row is 4 chars representing a hex number
numbers = hex2dec(strings); %// convert each row to a number
ind = numbers>=32768;
numbers(ind) = numbers(ind)-65535; %// get rid of jumps
plot(numbers)
thank a lot
Both previous answer were there too.
sscanf alternative assumed to be faster.

Convert an array of strings to a vector of numbers

I have this:
d = {'2.5', '3.5'};
d =
'2.5' '3.5'
How do I convert it to a vector of numbers?
d = 2.5 3.5
Given that you have a cell array of string values, you can use cellfun to apply str2num conversion on each entry. The output will be a vector/matrix of the converted values.
d = {'2.3','3.5'}
dVec = cellfun(#str2num, d);
However, you are better using str2double than str2num. From the documentation:
Note: str2num uses the eval function to convert the input argument.
Side effects can occur if the string contains calls to functions.
Using str2double can avoid some of these side effects.
dVec = cellfun(#str2double, d);
Update: As #yuk points out, you can call str2double() directly on the cell array for the same output.
dVec = str2double(d);
Note that the same call with str2num() will give the "must be a string or string array" error.