April 10, 2012, 2:51 am
Reads from a file called p, uses 10000 bins, filters out values < -10000. Sets a range of -10000 to 3500000, max value of 20.
[sourcecode language="python"]
#!/usr/bin/env python
import numpy as np
import matplotlib as mpl
import matplotlib.mlab as mlab
mpl.use('Agg')
import matplotlib.pyplot as plt
inp = open ("p","r")
x = []
for line in inp.readlines():
if int(line) > -10000:
x.append(int(line))
print x
# the histogram of the data
n, bins, patches = plt.hist(x, 10000, normed=0, facecolor='green')
print bins
print n
# add a 'best fit' line
#y = mlab.normpdf( bins, mu, sigma)
#l = plt.plot(bins, y, 'r--', linewidth=1)
plt.xlabel('Position')
plt.ylabel('Population')
plt.title('My data')
#plt.axis([-10000,3500000, 0, 20])
plt.grid(True)
plt.savefig('histogram.png')
[/sourcecode]
April 7, 2012, 8:56 pm
Messy and hacky, should use set rather than map. In reality should use a hash…
#include
#include
April 7, 2012, 2:42 pm
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <vector>
using namespace std;
void getrandline(string filename,size_t &selected_line_no,string &selected_line) {
ifstream file(filename.c_str());
selected_line_no = 0;
for(size_t n=1;!file.eof();n++) {
string current_line;
getline(file,current_line);
if(file.eof()) break;
if(rand()%n == 0) { selected_line = current_line; selected_line_no = n-1; }
}
}
int main(int argc,char **argv) {
srand(time(NULL));
vector<size_t> count(100,0); // just for testing, 100 should be > size of the file...
for(size_t n=0;n<100000;n++) {
size_t linenum;
string line;
getrandline(argv[1],linenum,line);
count[linenum]++;
cout << linenum << " " << line << endl;
}
for(size_t n=0;n<count.size();n++) {
cout << n << " " << count[n] << endl;
}
}
April 7, 2012, 1:40 am
I wonder if there is a better way…
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
void remonce(vector<int> &array) {
map<int,int> counts;
for(size_t n=0;n<array.size();n++) {
counts[array[n]]++;
}
vector<int> newarray;
for(size_t n=0;n<array.size();n++) {
if(counts[array[n]] != 1) {
newarray.push_back(array[n]);
}
}
array = newarray;
}
int main() {
vector<int> array;
array.push_back(4);
array.push_back(4);
array.push_back(3);
array.push_back(3);
array.push_back(8);
array.push_back(16);
array.push_back(8);
remonce(array);
for(size_t n=0;n<array.size();n++) {
cout << array[n] << endl;
}
}