transpose() { cat - | gawk -F, 'BEGIN{OFS=","}
NR == 1 {
	n = NF
	for (i = 1; i <= NF; i++)
		row[i] = $i
	next
}
{
	if (NF > n)
		n = NF
	for (i = 1; i <= NF; i++)
		row[i] = row[i] "," $i
}
END {
	for (i = 1; i <= n; i++)
		print row[i]

	}' 
}

sortCols() { cat - | gawk -F, 'BEGIN{FS=","; OFS=","}{for(i=1;i<=NF;i++) $i=$i; print $103,$95,$99,$92,$93,$96,$97,$98,$100,$101,$58,$59,$4,$5,$102,$13,$14,$94,$67,$68,$71,$85,$86,$17,$72,$78,$6,$70,$79,$7,$89,$76,$77,$90,$62,$8,$31,$32,$36,$84,$49,$50,$69,$87,$88,$63,$74,$9,$65,$18,$83,$56,$2,$27,$40,$41,$81,$82,$10,$47,$45,$73,$91,$53,$66,$3,$60,$54,$57,$35,$22,$23,$80,$26,$38,$11,$51,$16,$15,$44,$42,$52,$61,$39,$43,$34,$29,$48,$25,$24,$33,$12,$20,$30,$55,$64,$46,$19,$28,$37,$21,$75
}' 
}


for i in *mreValuesToWrite.txt;do
	rm transposed.dat
	rm sorted.dat
	echo  "${i%mreValuesToWrite.txt}" 
	cat $i | transpose > transposed.dat
	cat transposed.dat | sortCols > sorted.dat

cat sorted.dat |awk -F, '{OFS="\t"; for(i=1;i<=NF;i++) if ($i>= T1 && $i<T2) print NR,i}' T1=0 T2=0.5> 2d0.dat
cat sorted.dat |awk -F, '{OFS="\t"; for(i=1;i<=NF;i++) if ($i>= T1 && $i<T2) print NR,i}' T1=0.5 T2=1> 2d25.dat
cat sorted.dat |awk -F, '{OFS="\t"; for(i=1;i<=NF;i++) if ($i>= T1 && $i<T2) print NR,i}' T1=1 T2=1.5> 2d50.dat
cat sorted.dat |awk -F, '{OFS="\t"; for(i=1;i<=NF;i++) if ($i>= T1) print NR,i}' 		T1=1.5 > 2d75.dat

gnuplot <<EOF
set xlabel "Instance Indices" font "Times-Roman,20"
set ylabel "Pre-Processor+Method Indices" font "Times-Roman,20"
set title "MRE Values for Each Instance In ${i%mreValuesToWrite.txt} Dataset" font "Times-Roman,20"
set terminal postscript eps
set output "${i%mreValuesToWrite.txt}MREValues.eps"
set key bottom right
set size 2.5,2.5
set pointsize 1
plot "2d0.dat" title "0.5> & >=0" with points 0 0,\
"2d25.dat" title "1.0> & >=0.5" with points 0 6,\
"2d50.dat" title "1.5> & >=1" with points 4 ,\
"2d75.dat" title ">=1.5" with points 1 5  
EOF
epstopdf "${i%mreValuesToWrite.txt}MREValues.eps"
done

