title: For loop Python- from Matlab
body: <p>I am starting to code up in Python and I come from a Matlab background. I have a problem with a for loop that I am trying to do.</p>
  
  <p>So this is my for loop from <strong>Matlab</strong>,</p>
  
  <pre class="lang-matlab prettyprint-override"><code>ix = indoor(1);
  idx = indoor(2)-indoor(1);
  %Initialize X apply I.C
  X = [ix;idx];
  for k=(1:1:287)
      X(:,k+1) = Abest*X(:,k) + Bbest*outdoor(k+1) + B1best* (cbest4/cbest1);
  end
  </code></pre>
  
  <p>In this code Abest is a 2x2 matrix, Bbest is a 2x1 matrix, outdoor is a 288x1 vector, B1best is a 2x1 matrix. The matricies are found from a function using the matrix expodential command. c4 and c1 are terms defined before, constants.</p>
  
  <p>In <strong>Python</strong> I have been able to get the matrix exponential command to work in my function but I can't get that for loop to work.</p>
  
  <pre class="lang-py prettyprint-override"><code>Xo = np.array([[ix],[idx]])
  num1 = range(0,276)
  for k in num1:
      Xo[:,k+1] = Ae*Xo[:,k] + Be*outdoor[k+1] + Be1*(c4/c1)
  </code></pre>
  
  <p>Again Ae,Be,Be1 are matrices of the same size just like the Matlab ones. Same thing for the outdoor vector. </p>
  
  <p>I have tried everything I can think of to make it work... The only thing that worked for me was,</p>
  
  <pre class="lang-py prettyprint-override"><code>Xo = np.zeros(())
  #Initial COnditions
  ix = np.array(indoor[0])
  idx = np.array(indoor[1]-indoor[0])
  
  Xo = np.array([[ix],[idx]])
  #Range for the for loop
  num1 = range(0,1) 
  for k in num1:
      Xo = Ae*Xo[k] + Be*outdoor[k+1] + Be1*(c4/c1)
  </code></pre>
  
  <p>Now, this thing will work but only give me two points. If I change the range I get an error. I'm assuming this code works because my original Xo is just two states so k goes through those two states but that's not what I want.</p>
  
  <p>If anyone could help me out that would be very helpful! If I'm making some code error, it's honestly because I'm not understanding the 'For loop' in python to well when it comes to data analysis and having it loop through the rows and increment the columns. Thank you for your time.</p>
  
  <p>Upon Request here is my full code:</p>
  
  <pre><code>import scipy.io as sc
  import math as m
  import numpy as np
  import matplotlib.pyplot as plt
  import sys
  from scipy.linalg import expm, sinm, cosm
  import pandas as pd
  df = pd.read_excel('datatemp.xlsx')
  
  outdoor = np.array(df[['Outdoor']])
  indoor = np.array(df[['Indoor']])
  
  ###########################. FUNCTION DEFINE. #################################################
  #Progress bar
  def progress(count, total, status=''):
      percents = round(100.0 * count / float(total), 1)
      sys.stdout.write(' %s%s ...%s\r' % ( percents, '%', status))
      sys.stdout.flush()
  #Define Matrix for Model
  def Matrixbuild(c1,c2,c3):
      A = np.array([[0,1],[-c3/c1,-c2/c1]])
      B = np.array([[0],[1/c1]])
      B1 = np.array([[1],[0]])
      C = np.zeros((2,2))
      D = np.zeros((2,2))
  
      F = np.array([[0,1,0,1],[-c3/c1,-c2/c1,1/c1,0],[0,0,0,0],[0,0,0,0]])
      R = np.array(expm(F))
  
      Ae = np.array([[R.item(0),R.item(1)],[R.item(4),R.item(5)]])
      Be = np.array([[R.item(2)],[R.item(6)]])
      Be1 = np.array([[R.item(3)],[R.item(7)]])
      return Ae,Be,Be1;
  
  ###########################. Data. ################################################# 
  #USED FOR JUST TRYING WITHOUT ACTUAL DATA
  # outdoor = np.array([5.8115,4.394,5.094,5.1123,5.1224])
  # indoor = np.array([15.595,15.2429,15.0867,14.9982,14.8993])
  
  ###########################. Model Define. #################################################
  Xo = np.zeros((2,288))
  ix = np.array(indoor[0])
  idx = np.array(indoor[1])
  
  err_min = m.inf
  
  c1spam = np.linspace(0.05,0.001,30)
  c2spam = np.linspace(6.2,6.5,30)
  c3spam = np.linspace(7.1,7.45,30)
  totalspam = len(c1spam)*len(c2spam)*len(c3spam)
  ind = 0
  
  for c1 in c1spam:
              for c2 in c2spam:
                          for c3 in c3spam:
                                      c4 = 1.1
                                      #MatrixBuild Function
                                      result = Matrixbuild(c1,c2,c3)
                                      Ae,Be,Be1 = result
                                      Xo = np.array([ix,idx])
  
                                      Datarange = range(0,len(outdoor)-1,1)
  
                                      for k in Datarange:
                                              Xo[:,k+1] = np.matmul(Ae,Xo[:,k]) + np.matmul(Be,outdoor[k+1]) + Be1*(c4/c1)
  
                                      ind = ind + 1
                                      print(Xo)
                                      err = np.linalg.norm(Xo[0,range(0,287)]-indoor.T)
                                      if err&lt;err_min:
                                          err_min = err
                                          cbest = np.array([[c1],[c2],[c3],[c4]])
  
                                      progress(ind,totalspam,status='Done')
  # print(X)
  # print(err)
  # print(cbest)
  
  ###########################. Model with Cbest Values. #################################################
  c1 = cbest[0]
  c2 = cbest[1]
  c3 = cbest[2]
  result2 = Matrixbuild(c1,c2,c3)
  AeBest,BeBest,Be1Best = result2
  Xo = np.array([ix,idx])
  Datarange = np.arange(0,len(outdoor)-1)
  for k in Datarange:
              Xo[:,k+1] = np.matmul(AeBestb,Xo[:,k]) + np.matmul(BeBest,outdoor[k+1]) + Be1Best*(c4/c1)
  
  err = np.linalg.norm(Xo[0,range(0,287)]-indoor.T)
  print(cbest)
  print(err)
  
  ###########################. Plots. #################################################
  plt.figure(0)
  time = np.linspace(1,2,2)
  plt.scatter(time,X[0],s=15,c="blue")
  plt.scatter(time,indoor[0:2],s=15,c="red")
  plt.show()
  </code></pre>
  
  <p>And again my error occurs in the line with the for loop of 
  for k in Datarange:</p>
  
  <pre><code>Xo[:,k+1] = np.matmul(Ae,Xo[k]) + np.matmul(Be,outdoor[k+1]) + Be1*(c4/c1)
  </code></pre>
  
  <p>I was trying to use np.matmul for matrix multiplication but even without it, it wasn't working. </p>
  
  <p>If there are any other questions about my code please ask. Essentially I'm trying to find the best c1,c2,c3 coefficients that fit my data which is indoor temperature by using a basic second order constant coefficient model.</p>