What's wrong with this MATLAB function? -


i have function, fitness.m. function defined below:

function = fitness(par)     n = size(par,1)     l = size(par,2)     fitness_val = zeros(1,n);     i=1:n         j=1:l             fitness_val(i) = fitness_val(i) + str2num(par(i,j));         end     end     = fitness_val 

i executing code:

%par char array par =  1110001101 0110010001 1100010100 0110010111 1100111100 1100000101  fitness(par) 

my output should be

a =   6     4     4     6     6     4 

instead throws weird error this:

>> fitness(par) index exceeds matrix dimensions. 

what wrong code?

simply follows , not have worry index exceed matrix dimensions.

par cell

for = 1:numel(par)      fitness_val(i) = sum(par{i}=='1'); end 

this make assumption par cell contains strings, should not far stretch

or in function format

function fitval = fitness(par);     fitval = zeros(1,numel(par));     = 1:numel(par)         fitval(i) = sum(par{i}=='1');     end end 

par matrix

function fitval = fitness(par)     fitval = sum(par == '1',2).'; end 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -