3
votes

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

5

5 Answers

4
votes

You can use kron function

kron(triu(ones(28)),[1 ;1 ;1 ;1])
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.

Diagonal Mask

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;

Parabola Mask

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
1
votes

There are nice answers but maybe this one can be an alternative using triu function:

% image
img = rand(112, 28);

% utilize a square matrix to use triu command
temp = nan(112, 112);
temp(:, 1:4:end) = img;
temp = triu(temp, -3);

% put the relevant elements back
img = temp(:, 1:4:end);