help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Importing a sparse matrix to Octave


From: Carlo de Falco
Subject: Re: Importing a sparse matrix to Octave
Date: Fri, 24 Jul 2009 15:31:19 -0500


On 24 Jul 2009, at 14:06, Chong Yidong wrote:

"John W. Eaton" <address@hidden> writes:

 # name: s
 # type: sparse complex matrix
 # nnz: 70
 # rows: 10
 # columns: 10
 1 1 (0,1.145193428282681)
 2 1 (0,0.5663270399606521)
 3 1 (0,0.007270909230606313)
 4 1 (0,0.1480797228543177)
 5 1 (0,0.2478795113138578)
 ...

The first column is the row index, the second is the column index, and
the third is the corresponding value (real,complex).  The number of
elements must match the value of NNZ.  To load this file into Octave,
use the load command.

Thanks.  However, this method doesn't seem to work properly:

# name: s
# type: sparse complex matrix
# nnz: 3
# rows: 10
# columns: 10
1 1 (0,1.14)
3 6 (0,0.56)
6 2 (0.1,0.0)

octave:1> load "foo.dat"
octave:2> s
s =

Compressed Column Sparse (rows = 10, cols = 10, nnz = 3)

 (1, 1) ->  0.00000 + 1.14000i
 (3, 2) ->  0.00000 + 0.56000i
 (6, 2) ->  0.10000 + 0.00000i

The second line seems to be assigned to the wrong entry. Is this a bug?
I'm using Octave 3.0.1, as distributed by Ubuntu.

If your code is free software you could also link it directly to octave-lib and let octave-lib handle the file format by doing something like:


--------------------------- BEGIN save_example.cc ---------------------------
#include <octave/oct.h>
#include <octave/load-save.h>
#include <iostream>

extern bool save_ascii_data (std::ostream& os, const octave_value& val_arg,
                             const std::string& name, bool mark_as_global,
                             int precision);
int main (void) {
  SparseComplexMatrix M (10, 10, 3);
  M (0, 0) =   std::complex<double> (0, 1.14);
  M (2, 5) =   std::complex<double> (0, 0.56);
  M (5, 1) =   std::complex<double> (0.1, 0.0);

  const octave_value val (M);
  std::string name ("M");

  save_ascii_data   ( std::cout, val, name, 0, 10);

  return 0;
}
--------------------------- END save_example.cc ---------------------------

mkoctfile --link-stand-alone save_example.cc -o save_example
./save_example > saved_matrix
octave -q

>> load saved_matrix
>> M
M =

Compressed Column Sparse (rows = 10, cols = 10, nnz = 3 [3%])

  (1, 1) ->  0.00000 + 1.14000i
  (6, 2) ->  0.10000 + 0.00000i
  (3, 6) ->  0.00000 + 0.56000i


HTH,
c.

P.S. sorry for my very bad c++ style...


reply via email to

[Prev in Thread] Current Thread [Next in Thread]