Hrihaan
Hrihaan

Reputation: 285

Difference between a range of values of txt files

So I have 10 txt files named A_1,A_2,......A_10 and a working txt file named A. In each column of these txt files, there are 4320 values. My goal is to only compare the first 1440 values of any column of the A txt file with the other 10 txt files(A_1,A_2,.....A_10) and find the sum of square of differences. My approach is this but it gives me the difference of all the 4320 values, i am stuck at how to manipluate the code to find the difference of only the first 1440 values:

import numpy as np
filelist=[]
for i in range(1,11):
    filelist.append("/Users/Hrihaan/Desktop/A_%s.txt" %i)
for fname in filelist:
    data=np.loadtxt(fname)
    data1=np.loadtxt('/Users/Hrihaan/Desktop/A.txt')
    x=data[:,1]
    x1=data1[:,1]
    x2=(x-x1)**2
    x3=sum(x2)
    print(fname)
    print(x3)

Upvotes: 0

Views: 42

Answers (1)

E. Ducateme
E. Ducateme

Reputation: 4248

Adding the slice below should do the trick.

np.loadtxt(fname)[:1440]

It causes data to only include the rows indexed 0 up to but not including 1440... since Python is zero-based indexing, that gives you 1440 rows total.

for fname in filelist:
    data=np.loadtxt(fname)[:1440]
    data1=np.loadtxt('/Users/Hrihaan/Desktop/A.txt')
    x=data[:,1]
    x1=data1[:,1]
    x2=(x-x1)**2
    x3=sum(x2)
    print(fname)
    print(x3)

Upvotes: 1

Related Questions