create sparse upper triangular matrix with only 1s

10 views (last 30 days)
Hello. I want to create a sparse upper triangular with size 10^5x10^5. Is it possible?
m = triu(ones(10^5);
m = sparse(triu(ones(10^5));
exceed maximum array size preference. Thanks.

Accepted Answer

James Tursa
James Tursa on 7 Jun 2016
Edited: James Tursa on 7 Jun 2016
A sparse version of the full triangular matrix will have slightly over 1/2 of the values, plus some extra overhead for the explicit index numbers that are saved. A comparison of memory usage is:
n = number of rows & columns
full memory = 8*n^2 bytes
sparse memory = (8 + (4 or 8))*n*(n+1)/2 + (4 or 8)*(n+1) bytes
The (4 or 8) will depend on whether the indexing is saved as 4 or 8 byte integers on your machine.
So, if we assume 8 byte integers on your machine and n = 10^5, the memory comparison is:
full memory = 8e10 bytes
sparse memory = 8.0001600008e+10 bytes
The sparse version of this triangular matrix will actually consume more data memory than the full version.
A logical version of this matrix will consume this amount of data memory:
sparse logical memory = (1 + (4 or 8))*n*(n+1)/2 + (4 or 8)*(n+1) bytes
So for 8 byte indexing and n = 10^5 as above, you would get:
sparse logical memory = 4.5001250008e+10 bytes
Still 56% the size of the full double version, so probably not what you need.
You need to find another way to solve your problem.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!