translated matlab code until start of for loop