help-octave
[Top][All Lists]
Advanced

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

Re: Data Structure Question


From: Paul Kienzle
Subject: Re: Data Structure Question
Date: Sun, 11 Jan 2004 22:55:08 -0500

I believe you want to do this:

octave:729> x.data=[1,2,3;4,5,6];
octave:730> x.rows={'one';'two'};
octave:731> x.cols={'first','second','third'}
x =
{
  cols =
  {
    [1,1] = first
    [1,2] = second
    [1,3] = third
  }
  data =

    1  2  3
    4  5  6

  rows =
  {
    [1,1] = one
    [2,1] = two
  }
}

octave:732> x.data(1,2)
ans = 2
octave:733> x.rows{1}
ans = one
octave:734> x.cols{2}
ans = second

Paul Kienzle
address@hidden

On Jan 11, 2004, at 10:40 PM, Henry F. Mollet wrote:

Thanks. I used a comma instead of a semi-colon because I was hoping to get a row-vector for my variable names. Then I've tried your suggestion of using a
cell structure instead of a data structure (if this is the correct
terminology) and had difficulties with referencing my row comments and
variable names (see below with comments).
Henry

octave:48> mySpreadSheet
mySpreadSheet =

{
  [1,1] =
    1.0000  1.1000  2.1000  3.1000
    2.0000  1.2000  2.2000  3.2000
    3.0000  1.3000  2.3000  3.3000
    4.0000  1.4000  2.4000  3.4000
  [1,2] =
male
fem
male
female
  [1,3] =
Var1
Var2
Var3
Var4
}

octave:49> iscell (mySpreadSheet)
ans = 1  % OK
octave:51> mySpreadSheet{1}(1,3)
ans = 2.1000 %OK third value in first row
octave:52> mySpreadSheet{2}(1)
error: single index only valid for row or column vector
% looks like a col-vector to me?
octave:52> mySpreadSheet{2}(1,1)
ans = m % first letter of male but I was hoping to get "male"
% Does it imply that I can index with only one letter
% If so, I might as well index with a number using an
% additional col in my matrix?
octave:53> mySpreadSheet{3}(1)
error: single index only valid for row or column vector
% as above for row index
octave:53> mySpreadSheet{3}(1,1)
ans = V % as above for row index



on 1/10/04 1:08 PM, Paul Kienzle at address@hidden wrote:

You have a ';' in comment and a ',' varname which is why it doesn't work like you expect. You will be better off using {} rather than [] for an
array
of strings because you can then use x.comment{i} rather than
deblank(x.comment(i,:)) to reference them.  You need the deblank
for [] because a character matrix must be rectangular.  You don't
need it for {} because a vector of values can contain character vectors
of different lengths.

Paul Kienzle
address@hidden

On Jan 10, 2004, at 1:27 PM, Henry F. Mollet wrote:

Am I on the right track here for the use of a data structure? I'd like
to
add variable names (columns) and comments (rows) to a matrix a. It
seems to
work for the comments but not the varnames.
Henry


octave:13> x.a = [1,2;3,4];
octave:14> x.comment = ["FirstRowComment"; "SecondRowComment"];
octave:15> x.varname = ["Var1", "Var2"];
octave:16> x
x =
{
  a =

    1  2
    3  4

  comment =

FirstRowComment
SecondRowComment

  varname = Var1Var2
}



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------





-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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