Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please i need to change this to matlab programing import random source = \"jiKnp

ID: 3738750 • Letter: P

Question

Please i need to change this to matlab programing

import random

source = "jiKnp4bqpmAbp"

target = "Hello, World!"

def fitness(source, target):

fitval = 0

for i in range(0, len(source)):

fitval += (ord(target[i]) - ord(source[i])) ** 2

return(fitval)

def mutate(source):

charpos = random.randint(0, len(source) - 1)

parts = list(source)

parts[charpos] = chr(ord(parts[charpos]) + random.randint(-1,1))

return(''.join(parts))

random.seed()

fitval = fitness(source, target)

i = 0

while True:

i += 1

m = mutate(source)

fitval_m = fitness(m, target)

if fitval_m < fitval:

fitval = fitval_m

source = m

print "%5i %5i %14s" % (i, fitval_m, m)

if fitval == 0:

break

Explanation / Answer

source = "jiKnp4bqpmAbp";

target = "Hello, World!";

fitval = fitness(source, target);

i = 0;

while 1

   i = i + 1;

   m = mutate(source);

   fitval_m = fitness(m, target);

   if fitval_m < fitval

fitval = fitval_m;

source = m;

fprintf('%5i %5i %14s', i, fitval_m, m);

   end

   if fitval == 0

break

   end

end

function fitval = fitness(source, target)

   fitval = 0

   for i = 1 : length(source)

fitval = fitval + (double(target(i)) - double(source(i))) ^ 2;

   end

end

function parts = mutate(source)

   %charpos = random.randint(0, length(source) - 1)

   parts = source;

   charpos = randi(length(source)) - 1;

   %parts = list(source)

   parts(charpos) = char(double(parts(charpos)) + (randi(3)-2));

   %return(''.join(parts))

end