/*****************************************************************************/

if (NON_EMPTY_SELECTION)
{
	UPDATE_CELL_DATE = 0;
	
	data_rows  	 = Columns 	(SELECTED_CHART_ROWS);

	temp = SELECTED_CHART_COLS[0];

	for (count = 1; count<data_rows; count = count+1)
	{
		temp2 = SELECTED_CHART_COLS[count];
		if (temp!=temp2)
		{
			break;
		}
		if (SELECTED_CHART_DATA[count]<SELECTED_CHART_DATA[count-1])
		{
			break;
		}
	}

	if (count == data_rows)
	{
		/* compress the data vector to account for repeated observations */
		
		dataPoints = Columns(SELECTED_CHART_DATA);
		temp_data_vector = {2, dataPoints};
		currentIndex = 0;
		
		temp_data_vector[0][0] = SELECTED_CHART_DATA[0];
		temp_data_vector[1][0] = 1;
		for (nextIndex = 1; nextIndex < dataPoints; nextIndex = nextIndex + 1)
		{
			if (SELECTED_CHART_DATA[nextIndex]!=SELECTED_CHART_DATA[nextIndex-1])
			{
				currentIndex = currentIndex+1;
				temp_data_vector[0][currentIndex] = SELECTED_CHART_DATA[nextIndex];
			}
			temp_data_vector[1][currentIndex] = temp_data_vector[1][currentIndex] + 1;
		}
		
		SELECTED_CHART_DATA = {2, currentIndex+1};
		
		for (nextIndex = 0; nextIndex <= currentIndex; nextIndex = nextIndex + 1)
		{
			SELECTED_CHART_DATA[0][nextIndex] = temp_data_vector[0][nextIndex];
			SELECTED_CHART_DATA[1][nextIndex] = temp_data_vector[1][nextIndex];
		}
		
	
		
		patternCount = Columns(SELECTED_CHART_DATA);
		fprintf (stdout, "\n");
		upTo = Max(1,10);
		
		cMeans  = {};
		errMatrix = {upTo,5};

		kIters = 1000;
		
		for (cc = 0; cc < upTo; cc = cc+1)
		{
			km = Transpose(SELECTED_CHART_DATA) <= {{cc+1}{kIters}};
				
			cMeans[cc+1]  = km;

			errMatrix[cc][0] = cc+1;
			errMatrix[cc][1] = km[1][0];
			errMatrix[cc][2] = 1./km[1][0];
			if (cc)
			{
				errMatrix[cc][3] = errMatrix[cc][2]-errMatrix[cc-1][2];
				errMatrix[cc][2] = 1./km[1][0];
				errMatrix[cc][4] = 100*km[1][1]/kIters;
			}
			else
			{
				errMatrix[cc][4] = 100;			
			}
		}
		
		labels = {{"Classes", "Error", "Transformed Error", "Transformed Jumps", "Confidence"}};
		
		promptString = "Clustering Error";
		OpenWindow (CHARTWINDOW,{{promptString}
								   {"labels"},
								   {"errMatrix"},
								   {"Line Plot"},
								   {labels[0]},
								   {labels[3]},
								   {"Cluster Count"},
								   {""},
								   {"Transformed Error"},
								   {"0"}},
								   "SCREEN_WIDTH-60;SCREEN_HEIGHT-50;30;50");
								   
	}
	else
	{
		fprintf (stdout, "Please select a single sorted column to fit a profile to.\n");
	}
}
else
{
	NON_EMPTY_SELECTION = -1;
}
