How to recover a square matrix from its upper triangular cell part?

Hi all,
I'd like to compute the upper triangular part only for a symmetric matrix in a block by block way. After some operations I got this:
K>> tempBlk{:}
ans =
[6x6 double] [6x5 double] [6x5 double] [6x5 double]
[] [5x5 double] [5x5 double] [5x5 double]
[] [] [5x5 double] [5x5 double]
[] [] [] [5x5 double]
The cell blocks in the diagonal line here are full, symmetric, and the non-diagonal cell blocks are full, non-symmetric. The entire symmetric matrix result should be 21 by 21, which has the length and width of 6+5+5+5. I'd like to fill the empty cells (these [ ]) with zeros, then use cell2mat to transform it back to scalar matrix, then use triu to take only the upper triangular part.
Any idea of how to do this? Thanks!

 Accepted Answer

There's no reason to fill the [] cells with zeros. You can fill them with anything, since they will eventually be overwritten with zeros by triu. I would therefore do as follows:
idx=cellfun('isempty', yourCellArray);
C=cellfun(@transpose,yourCellArray.','uni',0);
yourCellArray(idx)=C(idx);
result = triu(cell2mat(yourCellArray))

5 Comments

If I fill these [] cells with sparse zero matrices, then turn the result tempBlk into sparse matrix, will I save some memory usage?
In your code, the line "yourCellArray(idx)=C(idx);" will fill the lower triangular part with full dense cells, is this unnecessarily expensive in storage if the symmetric matrix has a large size?
If I fill these [] cells with sparse zero matrices, then turn the result tempBlk into sparse matrix, will I save some memory usage?
No, you will not. In sparse form, additional data must be stored for every non-zero element in the matrix, in order to know its (i,j) location. If only half the elements are zero, there is no net gain, and possibly some loss, e.g.,
>> A=triu(rand(5000)); As=sparse(A); Whos A As
Name Size Kilobytes Class Attributes
A 5000x5000 195313 double
As 5000x5000 195391 double sparse
I understand. It true that for the case when only half the elements are zero, sparse doesn't really save any memory cost.
However, in my case, this symmetric matrix "result" has some zero rows and columns, which would make it cost less in memory with saving sparse matrix. The sparsity of "result" is:
For a 16 by 16 matrix, it's 2048 bytes when it's full, and 1592 bytes in my case when it's sparse.
Anyway, still, what I meant was in this operation:
yourCellArray(idx)=C(idx);
The lower triangular cells (the [ ]s) are filled with full, non-sparse matrices temporarily, which will be modified to zeros in triu.
Does it cost less memory if I fill these cells (the [ ]s) with something like "sparse(6, 5)" even only temporarily?
Probably, but it will amount to a 25% saving at best. It would be better if, instead of cell2mat, you just allocated the final matrix at the very beginning and wrote into the blocks.
Thank you, this is very helpful!

Sign in to comment.

More Answers (0)

Asked:

on 27 Jul 2017

Commented:

on 28 Jul 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!