Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
6.0.2 - Français

Change language to:
English - 日本語 - Português - Русский

Please note that the recommended version of Scilab is 2025.0.0. This page might be outdated.
See the recommended documentation of this function

Aide de Scilab >> Matrices creuses > Sparse Matrix Conversion > adj2sp

adj2sp

converts adjacency form into sparse matrix.

Syntax

A=adj2sp(xadj,iadj,v)
A=adj2sp(xadj,iadj,v,mn)

Arguments

xadj

a (n+1)-by-1 matrix of floating point integers. For j=1:n, the floating point integer xadj(j+1)-xadj(j) is the number of non zero entries in column j.

iadj

a nz-by-1 matrix of floating point integers, the row indices for the nonzeros. For j=1:n, for k = xadj(j):xadj(j+1)-1, the floating point integer i = iadj(k) is the row index of the nonzero entry #k.

v

a nz-by-1 matrix of floating point integers, the non-zero entries of A. For j=1:n, for k = xadj(j):xadj(j+1)-1, the floating point integer Aij = v(k) is the value of the nonzero entry #k.

mn

a 1-by-2 or 2-by-1 matrix of floating point integers, optional, mn(1) is the number of rows in A, mn(2) is the number of columns in A. If mn is not provided, then mn=[m,n] is the default with m=max(iadj) and n=size(xadj,"*")-1.

A

m-by-n real or complex sparse matrix (with nz non-zero entries)

Description

adj2sp converts a sparse matrix into its adjacency form format. The values in the adjacency format are stored column-by-column. This is why this format is sometimes called "Compressed sparse column" or CSC.

Examples

In the following example, we create a sparse matrix from its adjacency format. Then we check that it matches the expected sparse matrix.

xadj = [1 3 4 7 9 11]';
iadj = [3 5 3 1 2 4 3 5 1 4]';
v = [1 2 3 4 5 6 7 8 9 10]';
B=adj2sp(xadj,iadj,v)
A = [
0 0 4 0 9
0 0 5 0 0
1 3 0 7 0
0 0 6 0 10
2 0 0 8 0
];
C=sparse(A)
and(B==C)

In the following example, we create a sparse matrix from its adjacency format. Then we check that it matches the expected sparse matrix.

xadj = [1 2 3 4 5 5 6 6 7 8 9]';
iadj = [2 5 2 3 1 2 7 6]';
v = [3 7 5 3 6 5 2 3]';
C=adj2sp(xadj,iadj,v)
A = [
0 0 0 0 0 6 0 0 0 0
3 0 5 0 0 0 0 5 0 0
0 0 0 3 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 7 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 3
0 0 0 0 0 0 0 0 2 0
];
B=sparse(A)
and(B==C)

In the following example, we check the use of the mn parameter. The consistency between the mn parameter and the actual content of xadj and iadj is checked by adj2sp.

xadj = [1 2 3 4 5 5 6 6 7 8 9]';
iadj = [2 5 2 3 1 2 7 6]';
v = [3 7 5 3 6 5 2 3]';
mn=[7 10];
C=adj2sp(xadj,iadj,v,mn)

In the following example, create a 3-by-3 sparse matrix. This example is adapted from the documentation of SciPy.

xadj =  [1,3,4,7]
iadj =  [1,3,3,1,2,3]
v = [1,2,3,4,5,6]
full(adj2sp(xadj,iadj,v))

The previous script produces the following output.

-->full(adj2sp(xadj,iadj,v))
 ans  =
    1.    0.    4.
    0.    0.    5.
    2.    3.    6.

In the following example, we check that the sp2adj and adj2sp functions are inverse.

// Templates for the Solution of Algebraic Eigenvalue Problems: a Practical Guide
// Edited by Zhaojun Bai, James Demmel, Jack Dongarra, Axel Ruhe, and Henk van der Vorst
// "Sparse Matrix Storage Formats", J. Dongarra
// http://web.eecs.utk.edu/~dongarra/etemplates/book.html

A = [
10 0 0 0 -2 0
3 9 0 0 0 3
0 7 8 7 0 0
3 0 8 7 5 0
0 8 0 9 9 13
0 4 0 0 2 -1
];
A = sparse(A)

// To get the Compressed Sparse Column (CSC) :
[col_ptr,row_ind,val]=sp2adj(A)
// To convert back to sparse:
AAsp=adj2sp(col_ptr,row_ind,val)
// Check the conversion
AAsp - A

// To get the Compressed Sparse Row (CSR) :
[row_ptr,col_ind,val]=sp2adj(A')
// To convert back to sparse:
AAsp=adj2sp(row_ptr,col_ind,val)'
// Check the conversion
AAsp - A

See also

  • sp2adj — converts sparse matrix into adjacency form
  • sparse — sparse matrix definition
  • spcompack — converts a compressed adjacency representation
  • spget — retrieves entries of sparse matrix

References

"Implementation of Lipsol in Scilab", Hector E. Rubio Scola, INRIA, Decembre 1997, Rapport Technique No 0215

"Solving Large Linear Optimization Problems with Scilab : Application to Multicommodity Problems", Hector E. Rubio Scola, Janvier 1999, Rapport Technique No 0227

"Toolbox Scilab : Detection signal design for failure detection and isolation for linear dynamic systems User's Guide", Hector E. Rubio Scola, 2000, Rapport Technique No 0241

"Computer Solution of Large Sparse Positive Definite Systems", A. George, Prentice-Hall, Inc. Englewood Cliffs, New Jersey, 1981.

Report an issue
<< Sparse Matrix Conversion Sparse Matrix Conversion full >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Feb 14 14:59:57 CET 2019