help-octave
[Top][All Lists]

## Re: View matrix in CSR / CSC format

 From: Bård Skaflestad Subject: Re: View matrix in CSR / CSC format Date: Fri, 4 Dec 2015 13:17:48 +0100 User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0

```On 04/12/15 12:31, Martin Beseda wrote:
```
```Thank you very much!
```
```
```
You should reply to the mailing list too (and keep your reply below the text you're replying to). That would make it easier for others to comment and to point out the mistake I made in my original response. Obviously, you don't get the CSC representation by counting the row indices.
```
```
I'll atone for that mistake by making the reply more complete. Here are a couple of functions for converting between Octave's sparse matrices and the CSR and CSC representations:
```
function [ia, ja, sa] = sparse_to_csr(A)
[ia, ja, sa] = sparse_to_csc(A .');
end

function A = csr_to_sparse(ia, ja, sa)
A = spconvert([expand(ia), ja, sa]);
endfunction

function [p, i, x] = sparse_to_csc(A)
[i, j, x] = find(A);
p = cumsum([1 ; accumarray(j, 1)]);
endfunction

function A = csc_to_sparse(p, i, x)
A = spconvert([i, expand(p), x]);
endfunction

function k = expand(p)
k = zeros([p(end) - 1, 1]);
k(p(1 : end - 1)) = 1;

k = cumsum(k);
endfunction

Sincerely,
--