How to define an empty character array in matlab?

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

The above code gives the following error:

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

I need an empty array of characters with a name chromosomes. I tried adding the following line before the indicated loops.

chromosomes(1:POPULATION_SIZE)='';

but does not work. It gives an error

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

+3
source share
1 answer

Do you want the chromosomes to be a character array (when all rows are the same size) or an array of cells (with a variable size of the ith element)?

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 into the for loop:

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

In the case of an array of cells:

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

DEC2BIN . parameters i- . :

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

Source: https://habr.com/ru/post/1747891/


All Articles