Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting rid of the upper or lower triangular part of a symmetric matrix

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

like image 953
Ilkay Isik Avatar asked Feb 01 '26 10:02

Ilkay Isik


2 Answers

You can use kron function

kron(triu(ones(28)),[1 ;1 ;1 ;1])
like image 146
rahnema1 Avatar answered Feb 03 '26 00:02

rahnema1


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));
like image 28
Suever Avatar answered Feb 03 '26 00:02

Suever