help-octave
[Top][All Lists]
Advanced

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

Re: Attaching lables to matrices


From: Przemek Klosowski
Subject: Re: Attaching lables to matrices
Date: Wed, 16 Apr 2008 13:23:44 -0400 (EDT)

   I would like to be able to read a file like...

   ,  C1, C2
   R1, 1,  2
   R2, 3,  4

   And have a matrix [1,2;3,4] with the associated names attached.

What would you like to attach them to? As you know, Octave's native
data type is a numerical array so you can easily have an array A = [ 1
2 ; 3 4 ], and the row and column are implicit: A(1,:) is the first
row [ 1 2 ], while A(:,2) is the second column [ 2 4 ]'.

Are your files always ordered i.e. the numbers in C1 C2 ... and R1 R2 ...
are consecutive integers? If so, then you can just skip the labels
and load the array. Otherwise, you need to tease out the numbers from
the labels, and assign the elements of your result array by hand:

    <read column indices into a vector variable C>
    <loop over remaining lines>
       <get the row index number into a scalar R>
       A(R,C)=<vector of numbers in this line>

Depending on the details of your file format, etc, the
implementation of the above pseudocode might be as follows:

      FID = fopen ("splat.dat", "r");
      [c,size]=fscanf(FID," , C%d , C%d");
      [ar,size] = fscanf(FID," R%d, %d, %d ",[3,Inf]);
      r=ar(1,:);
      A(r',c')=ar(2:end,:)';




reply via email to

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