Convert Integer Array to Binary Array

조회 수: 33 (최근 30일)
Igor Felipe Gallon
Igor Felipe Gallon 2015년 7월 15일
댓글: dpb 2019년 5월 17일
Hello, people.
I want to convert an Integer array to a Binary array but i don't know how to do this. I've read about the function "dec2bin" but it doesn't do what i exactly want (i think...). For example (sorry my bad artistic skills):
So, thank you!
  댓글 수: 2
Image Analyst
Image Analyst 2015년 7월 15일
편집: Image Analyst 2015년 7월 15일
Seems weird to me that all numbers don't have the same number of characters. So the binary number tells you nothing about what created it. For example 1011 could come from both 23 and from 51. What good is that? Why do you want to do this?
Igor Felipe Gallon
Igor Felipe Gallon 2015년 7월 16일
For each element in the integer array "int" I save the required number of bits used in another array "nBits". For example, nBits(i) = numel(dec2bit(int(i))). So with this i can recover the right value after the binary conversion.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2015년 7월 16일
편집: Andrei Bobrov 2015년 7월 16일
intArray -> (bitArray, nBit)
intArray = [3 2 1 7 6 2 5];
[~,nBit] = log2(intArray);
bitArray = cell2mat(arrayfun(@dec2bin,V,'un',0));
(bitArray, nBit) -> intArray
intArray = cellfun(@bin2dec,mat2cell(bitArray,1,nBit));
  댓글 수: 3
Nehemias Rodriguez
Nehemias Rodriguez 2019년 5월 17일
편집: Nehemias Rodriguez 2019년 5월 17일
it says undefined V. help please. my array is A = zeros(1, 50);
dpb
dpb 2019년 5월 17일
That was a typo/transcription error from Andrei's desktop to the forum Answer -- he obviously had an array V he used to test and missed getting all references changed to the OPs variable when pasted code into Answer.
V is intArray for the example...

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

dpb
dpb 2015년 7월 15일
Using builtins w/o mod/rem to separate digits...
>> dec2bin(str2num(sprintf('%d',x).'))
ans =
011
010
001
111
110
010
101
>>
  댓글 수: 4
Igor Felipe Gallon
Igor Felipe Gallon 2015년 7월 16일
There aren't null elements in the integer array. I want the binary array in this format because i'm working with genetic algorithms and each integer array is a chromosome. To apply the mutation operator, i wish to use the binary representation because i think is easier to manipulate (random switch, for example) data this way. Thank you!
Walter Roberson
Walter Roberson 2015년 7월 16일
For GA work you will very likely want fixed-length segments. With the scheme you are proposing, after you do the crossover, you do not know how to deconstruct it into integers again, other than the fact that you know that in this scheme no integer will start with a binary 0.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2015년 7월 16일
V = 3217625;
d = sprintf('%d', V) - '0';
b = dec2bin(d) .' - '0';
one_seen = cumsum(b,1) > 0;
bits = b(one_seen) .';
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2015년 7월 16일
nBit = sum(one_seen);
Walter Roberson
Walter Roberson 2015년 7월 16일
You are relying upon accidents of your encoding scheme. If you were to renumber the entries in an array then you would change which mutated states are possible. For example if you were to initialize 20 different array indices all to 1, then those would encode as 1 bit each, and your mutation cannot create entries that represent 2 or higher because additional bits would be required to store those indices.
If your swapping can swap in the "middle" of a symbol then you could end up swapping a 0 from the end of 1 symbol to become the beginning of another symbol, but then you would not be able to decode that. For example, 10|1 with the 2nd and 3rd bits swapped would be 11|0 but 0 is not a valid encoding for any number in your scheme.
If your swapping is restricted to symbol boundaries then you have not gained anything other than complexity of representation compared to using a fixed number of bits to represent the indices.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by