function  [A,D,order,Limits,EndMembers,Conversion,Endbase,EndMemberCations,AdditiveExchangeVector] = shorten(A,C,D,...
order,thelabels,Limits,EndMembers,Conversion,Endbase,EndMemberCations,AdditiveExchangeVector)
%[A,D,order,Limits,EndMembers,Conversion,EndBase,EndMemberCations,AdditiveExchangeVedctor] = shorten(A,C,D,...
%order,thelabels,Limits,EndMembers,Conversion,EndBase,EndMemberCations,AdditiveExchangeVector)
%The Purpuse of shorten is the shrink a all of the vectors and matrices used for data conversion.  This 
%is required becuase if the system is compositional restricted then all of the additive/exchange, weight 
%and cation components defined for a mineral model may not be neccessary
%
%INPUT/OUTPUT   
% A = The matrix used for least squares conversion (double array)
% C = The a vector containing the additive quantities (double array)
% D = Weighting vector for input cations and additive quantities (double array)
% Limits = Limits for exchange values (double array)
% Endmembers = Names of Endmembers used for final calculation (cell array of strings)
% Conversion = Matrix that converts exchange components to endmember components (double array)
% Endbase = a vector showing which endmember is the additive component (double vector)
% EndMemberCations = The endmembers defined as cations (double array)
% AdditiveExchangeVector = The list of additive and exchange components for this phase (cell array of strings)
% All of these data are shortened based on the values held in the thelabels
%
%INPUT
% thelabels = a cell array of strings that contains the names of the elements to be considered.  All
% data is shortened based on this information
% order = The order of cation components in the A matrix
%
%Written by K. Steffen (ksteffen@unm.edu) at the University of New Mexico,
%Department of Earch and Planetary Science
%Copyright 2003 Kurt Steffen
%
%Last Modified June 17, 2003

if ~isempty(C)
	thelabels = [thelabels C(1,:)];
end

orderlength = length(order);
order = GetColumnsGeneral3(thelabels,order);%finding matches between thelabels and order
count = 1;
temp = [];
temp2 = [];
for k = orderlength:-1:1  %if there is not a match then trim that row from A and other 
	if order{2,k} == -1       %corrasponding data
		temp(count,:) = A(k,:);
		order(:,k) = [];
		A(k,:) = [];
		temp2(:,count) = EndMemberCations(:,k);
		EndMemberCations(:,k) = [];
		D(k) = [];
		count = count + 1;
	end
end


if count > 2    %This finds which columns of A need to be removed by seeing which columns
	Sumtemp = sum(abs(temp));  %the removed rows affected
	Sumtemp2 = sum(abs(temp2'));
else 
	Sumtemp = abs(temp);
	Sumtemp2 = abs(temp2');
end

if min(Sumtemp2 > 0)  %In this case all information should be removed, this is a problem
	EndMembers = [];
	EndMemberCations = [];
	Conversion = [];
	Endbase = [];
else
	EndMembers(find(Sumtemp2 > 0)) = [];  %otherwise removing the columns
	Endbase(find(Sumtemp2 > 0)) = [];
	EndMemberCations(find(Sumtemp2 > 0),:) = [];
	Conversion(:,find(Sumtemp2 > 0)) = [];
end	

if max(Endbase) == 0  %Had to remove the additive component, this is also a problem
	EndMembers = [];
end

if length(EndMembers) == 1 %If there are more then one endmembers then there is a solid solution
	SolidSolution = 0;
elseif length(EndMembers) > 1
	SolidSolution = 1;
else  %Had to remove all of the endmembers, this is a problem
	SolidSolution = -1;
end

for k = length(Sumtemp):-1:1  %moving more column information
	if Sumtemp(k) ~= 0;
		AdditiveExchangeVector(k) = [];
		A(:,k) = [];
		Limits(:,k) = [];
		if SolidSolution > -1
			Conversion(k,:) = [];
		end
	end
end
if SolidSolution == -1  %Setting everything to Null if there are no endmembers
	AdditiveExchangeVector = [];
	A = [];
	D = [];
	order = [];
	Limits = [];
	EndMembers = [];
	EndMemberCations = [];
end