I have a symmetric matrix of some statistical values that I want to plot using imagesc in Matlab. The size of the matrix is 112 X 28, meaning I want to display 4 rows for each column. How can I get rid of the upper or lower triangular part of this matrix ? Since this means deleting 4 rows per each column diagonally tril or triu functions does not work (they are for square matrices). Thanks
3
votes
5 Answers
3
votes
If you have the Image Processing Toolbox you could use imresize
to resize an upper triangular mask that you can then use to select the appropriate data
msk = imresize(triu(true(min(size(a)))), size(a), 'nearest');
% Just zero-out the lower diag
zeroed = msk .* a;
% Select the elements in the upper diagonal
upperdiag = a(msk);
If you don't have the Image Processing Toolbox (and imresize
) you can do something like
msk = reshape(repmat(permute(triu(true(min(size(a)))), [3 1 2]), size(a,1)/size(a,2), 1), size(a));
2
votes
I came up with a solution using meshgrid
First define a grid which covers all the indices of your matrix
[X, Y] = meshgrid([1:28], [1:112]);
You want to mask all values above (or below) the diagonal 4x = y. Just define the mask to be the function of your X and Y values.
mask = 4.*X >= Y; %>= Selects above the diagonal, <= selects below the diagonal
Here's the mask. Notice that the axises are not symmetric.
You can use this method to define any separating line or function on the grid. You could even do a parabola
mask_parabola = (X-14).^2 >= Y;
2
votes
You can create the mask with bsxfun
as follows:
M = 112; % number of rows
N = 28; % number of columns
mask = bsxfun(@le, (1:M).', (1:N)*round(M/N)); % create mask
data = data.*mask; % multiply your data matrix by the mask