Search code examples
matlabmatrixcolor-mapping

create color map blue scale


Hello I have a matrix

A=[
1 1 1;
1 1 2;
1 1 3;
1 2 1;
1 2 2;
1 2 3;
2 1 1;
2 1 2;
2 1 3;
2 2 1;
2 2 2;
2 2 3]

containing the Cartesian Product of the elements of the vectors V1 = [1 2], V2 = [1 2], V3 =[1 2 3] In particular each row of the matrix A is a combination of the elements of V1 V2 V3

Now I would like to create a color map in blue scale where the very light blue correspond to the 1st row [1 1 1] and the very dark blue correspond to the 12th row [2 2 3].

My questons:

1) How do I order the intermediate rows in a smart way such as they take intermediate blues? (summing over the rows?)

2) How can I create such blue color map?

Thanks!!!


Solution

  • See if this works for you -

    [sorted_sumcols,idx] = sort(sum(A,2)) %// sum over columns and sort based on the sum
    Aout = A(idx,:) %// Aout holds the re-ordered rows of A
    

    Output -

    Aout =
         1     1     1
         1     1     2
         1     2     1
         2     1     1
         1     1     3
         1     2     2
         2     1     2
         2     2     1
         1     2     3
         2     1     3
         2     2     2
         2     2     3
    

    For verification, you can look at the values of

    sorted_sumcols =
         3
         4
         4
         4
         5
         5
         5
         5
         6
         6
         6
         7
    

    So, that [1 1 2] belongs to the same group of [2 1 1], as both of their sorted_sumcols values are 4 at their row numbers 2 and 4.


    Edit

    This section of code extends the earlier code to keep same intensity (column-3) for same group elements -

    [~,~,ID] = unique(sorted_sumcols) %// IDs for each group
    meanvals = accumarray(ID,Aout(:,3),[],@mean) %// mean values for each group
    Aout(:,3) = meanvals(ID) %// replicate the mean values to each element
    out = Aout./max(Aout(:)) %// finally divide by max of all elements to keep 
                                 %//it in [0 1] as needed for custom colormaps
    

    Output -

    >> Aout
    Aout =
                             1                         1                         1
                             1                         1          1.33333333333333
                             1                         2          1.33333333333333
                             2                         1          1.33333333333333
                             1                         1                         2
                             1                         2                         2
                             2                         1                         2
                             2                         2                         2
                             1                         2          2.66666666666667
                             2                         1          2.66666666666667
                             2                         2          2.66666666666667
                             2                         2                         3
    
    >> out
    out =
             0.333333333333333         0.333333333333333         0.333333333333333
             0.333333333333333         0.333333333333333         0.444444444444444
             0.333333333333333         0.666666666666667         0.444444444444444
             0.666666666666667         0.333333333333333         0.444444444444444
             0.333333333333333         0.333333333333333         0.666666666666667
             0.333333333333333         0.666666666666667         0.666666666666667
             0.666666666666667         0.333333333333333         0.666666666666667
             0.666666666666667         0.666666666666667         0.666666666666667
             0.333333333333333         0.666666666666667         0.888888888888889
             0.666666666666667         0.333333333333333         0.888888888888889
             0.666666666666667         0.666666666666667         0.888888888888889
             0.666666666666667         0.666666666666667                         1