bmailhe@200: function [colors nbColors] = dico_color_separate(dico, mu) bmailhe@200: % DICO_COLOR cluster several dictionaries in pairs of high correlation bmailhe@200: % atoms. Called by dico_decorr. bmailhe@200: % bmailhe@200: % Parameters: bmailhe@200: % -dico: the dictionaries bmailhe@200: % -mu: the correlation threshold bmailhe@200: % bmailhe@200: % Result: bmailhe@200: % -colors: a cell array of indices. Two atoms with the same color have bmailhe@200: % a correlation greater than mu bmailhe@200: bmailhe@200: bmailhe@200: numDico = length(dico); bmailhe@200: colors = cell(numDico,1); bmailhe@200: for i = 1:numDico bmailhe@200: colors{i} = zeros(length(dico{i}),1); bmailhe@200: end bmailhe@200: bmailhe@200: G = cell(numDico); bmailhe@200: bmailhe@200: % compute the correlations bmailhe@200: for i = 1:numDico bmailhe@200: for j = i+1:numDico bmailhe@200: G{i,j} = abs(dico{i}'*dico{j}); bmailhe@200: end bmailhe@200: end bmailhe@200: bmailhe@200: % iterate on the correlations higher than mu bmailhe@200: c = 1; bmailhe@200: [maxCorr, i, j, m, n] = findMaxCorr(G); bmailhe@200: while maxCorr > mu bmailhe@200: % find the highest correlated pair bmailhe@200: bmailhe@200: % color them bmailhe@200: colors{i}(m) = c; bmailhe@200: colors{j}(n) = c; bmailhe@200: c = c+1; bmailhe@200: bmailhe@200: % make sure these atoms never get selected again bmailhe@200: % Set to zero relevant lines in the Gram Matrix bmailhe@200: for j2 = i+1:numDico bmailhe@200: G{i,j2}(m,:) = 0; bmailhe@200: end bmailhe@200: bmailhe@200: for i2 = 1:i-1 bmailhe@200: G{i2,i}(:,m) = 0; bmailhe@200: end bmailhe@200: bmailhe@200: for j2 = j+1:numDico bmailhe@200: G{j,j2}(n,:) = 0; bmailhe@200: end bmailhe@200: bmailhe@200: for i2 = 1:j-1 bmailhe@200: G{i2,j}(:,n) = 0; bmailhe@200: end bmailhe@200: bmailhe@200: % find the next correlation bmailhe@200: [maxCorr, i, j, m, n] = findMaxCorr(G); bmailhe@200: end bmailhe@200: bmailhe@200: % complete the coloring with singletons bmailhe@200: % index = find(colors==0); bmailhe@200: % colors(index) = c:c+length(index)-1; bmailhe@200: nbColors = c-1; bmailhe@200: end bmailhe@200: bmailhe@200: function [val, i, j, m, n] = findMaxCorr(G) bmailhe@200: %FINDMAXCORR find the maximal correlation in the cellular Gram matrix bmailhe@200: % bmailhe@200: % Input: bmailhe@200: % -G: the Gram matrix bmailhe@200: % bmailhe@200: % Output: bmailhe@200: % -val: value of the correlation bmailhe@200: % -i,j,m,n: indices of the argmax. The maximal correlation is reached bmailhe@200: % for the m^th atom of the i^th dictionary and the n^h atom of the bmailhe@200: % j^h dictionary bmailhe@200: bmailhe@200: val = -1; bmailhe@200: for tmpI = 1:length(G) bmailhe@200: for tmpJ = tmpI+1:length(G) bmailhe@200: [tmpVal tmpM] = max(G{tmpI,tmpJ},[],1); bmailhe@200: [tmpVal tmpN] = max(tmpVal); bmailhe@200: if tmpVal > val bmailhe@200: val = tmpVal; bmailhe@200: i = tmpI; bmailhe@200: j = tmpJ; bmailhe@200: n = tmpN; bmailhe@200: m = tmpM(n); bmailhe@200: end bmailhe@200: end bmailhe@200: end bmailhe@200: end bmailhe@200: