tags:

views:

38

answers:

1
for i=1:POPULATION_SIZE
    for j=1:NO_PARAMETERS
        c=dec2bin(parameters(j),NO_BITS_PARAMETER);
        chromosomes(i) = [chromosomes(i) c];
    end
end

above code is giving the following error:

??? Undefined function or method 'chromosomes' for input arguments of type 'double'.

I need an empty array of characters named chromosomes. I tried adding following line before the above loops.

chromosomes(1:POPULATION_SIZE)='';

but its not working. It gives error

??? Index of element to remove exceeds matrix dimensions.

+3  A: 

Do you want chromosomes to be character array (when all rows have the same size) or cell array (with variable size of ith elements)?

In the first case you define the variable as:

chromosomes = char(zeros(POPULATION_SIZE,NO_PARAMETERS*NO_BITS_PATAMETER));

or

chromosomes = repmat(' ',POPULATION_SIZE,NO_PARAMETERS*NO_BITS_PATAMETER); 

Then in for loop:

chromosomes(i,(j-1)*NO_BITS_PATAMETER+1:j*NO_BITS_PATAMETER) = c;

In the case of cell array:

chromosomes = cell(POPULATION_SIZE, NO_PARAMETERS); % each paramater in separate cell
for i=1:POPULATION_SIZE
    for j=1:NO_PARAMETERS
        c=dec2bin(parameters(j),NO_BITS_PARAMETER);
        chromosomes{i,j} = c;
    end
end

or

chromosomes = cell(POPULATION_SIZE,1); % all parameters in a single cell per i
for i=1:POPULATION_SIZE
    for j=1:NO_PARAMETERS
        c=dec2bin(parameters(j),NO_BITS_PARAMETER);
        chromosomes{i} = [chromosomes{i} c];
    end
end

EDIT:

Actually you can apply DEC2BIN to the whole array of numbers at once. It also looks like variable parameters are the same for every ith row. Then you can do:

c = dec2bin(parameters,NO_BITS_PARAMETER);
chromosomes = reshape(c',1,[]);
chromosomes = repmat(chromosomes,POPULATION_SIZE,1);
yuk

related questions