Tariq Islam
Tariq Islam

Reputation: 23

how to store 00 as 00 instead of 0 only

MATLAB stores 00 and 01 as 0 and 1 respectively. How can I make MATLAB store 00 as 00 and 01 as 01 instead of 0 and 1 only...here is my code.. I am talking about the statements with <-- only..In fact I want to input the result as initial population(chromosome) to a genetic algorithm.

function [x]=abc()
r=randi([0 3],1,20);
for i=1:20 
       if r(i)==0
            x(i)=00; %// <--
        elseif r(i)==1 
            x(i)=01; %// <--
        elseif r(i)==2
           x(i)=10;
        elseif r(i)==3 
            ex(i)=11;
        end 
    end
end

Upvotes: 0

Views: 237

Answers (1)

Hoki
Hoki

Reputation: 11792

It looks like you want to store the binary representation of your numbers, so you can use the function dec2bin

and the best thing, you don't even need a loop ;)

r=randi([0 3],1,20);
x = dec2bin(r,2) ;

>> x
x =
10
00
11
11
10
11
10
01
...

Upvotes: 4

Related Questions