Search code examples
matlabsparse-matrixspy

Greater weight to smaller values using `spy` in Matlab?


I'm new to MatLab and I have a table of a few hundred variables. I know that the smaller variables hold greater significance than the larger variables in that table and want the sparse matrix I graph to illustrate this. Not so much lim approaches 0 but as the lim approaches 1 because I know the most significant values all approach 1. Do I just take the inverse of that matrix?


Solution

  • Note that spy is a way to visualize the sparsity pattern of a matrix, but does not let you visualize the value. For that, imagesc is a good candidate.

    It would help to have more information about the problem, but here is one way you could illustrate the importance of values closer to 1.

    % Generate a random 10x10 matrix with 50% sparsity in [0,9]
    x = 9*sprand(10,10,0.5);
    % Add 1 to non-zero elements so they are in the range [1,10]
    x = spfun(@(a) a+1, x);
    % Visualize this matrix
    figure(1); imagesc(x); colorbar; colormap('gray');
    
    % Create the "importance matrix", which inverts non-zero values.
    % The non-zero elements will now be in the range [1/10,1]
    y = spfun(@(a) 1./a, x);
    % Visualize this matrix
    figure(2); imagesc(y); colorbar; colormap('gray');
    

    edit to address comment:

    % If you want to see values that are exactly 1, you can use
    y = spfun(@(a) a==1, x);
    % If you want the inverse distance from 1, use
    y = spfun(@(a) 1./(abs(a-1)+1), x);