How do I translate Matlab's colon into Octave?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I translate Matlab's colon into Octave?

Dirk Laurie
I've got zillions of old Matlab 3.5 routines that rely on one of the
following devices:

  1.  x(:) returns the contents of x as a column vector
  2.  y(:)=x assigns the contents of x to y, retaining the
      present shape of y

It seems that these tricks do not work in Octave.  Are there
alternatives that do not involve massive re-coding?

Dirk Laurie


Reply | Threaded
Open this post in threaded view
|

Re: How do I translate Matlab's colon into Octave?

Jeppe Sigbrandt

> From: Dirk Laurie <[hidden email]>
> I've got zillions of old Matlab 3.5 routines that rely on one of the
> following devices:
>
>   1.  x(:) returns the contents of x as a column vector
>   2.  y(:)=x assigns the contents of x to y, retaining the
>       present shape of y

Dirk,

Am not sure I understand the problem.  Isn't this doing
what you ask?

octave:2> x=1:10;
octave:3> x(:)
ans =
 
   1   2   3   4   5   6   7   8   9  10
 
octave:4> y=zeros(10,1);
octave:5> y(:)=x
y =
 
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
 

Notice that I deliberately tried to confuse octave
by feeding y (10 rows, 1 column) the vector x
(10 columns, 1 row).

Regards,
jay
 


Reply | Threaded
Open this post in threaded view
|

How do I translate Matlab's colon into Octave?

John W. Eaton-6
In reply to this post by Dirk Laurie
On  5-Sep-1997, Dirk Laurie <[hidden email]> wrote:

| I've got zillions of old Matlab 3.5 routines that rely on one of the
| following devices:
|
|   1.  x(:) returns the contents of x as a column vector
|   2.  y(:)=x assigns the contents of x to y, retaining the
|       present shape of y
|
| It seems that these tricks do not work in Octave.  Are there
| alternatives that do not involve massive re-coding?

Invoke Octave with --traditional:

  bevo:2> octave --traditional
  Octave, version 2.0.9 (alpha-dec-osf3.2).
  Copyright (C) 1996, 1997 John W. Eaton.
  This is free software with ABSOLUTELY NO WARRANTY.
  For details, type `warranty'.

  >> x = [1,2,3;4,5,6]
  x =

    1  2  3
    4  5  6

  >> x(:)
  ans =

    1
    4
    2
    5
    3
    6

  >> y = [6,5;4,3;2,1]
  y =

    6  5
    4  3
    2  1

  >> y(:) = x
  y =

    1  5
    4  3
    2  6


Or, if you just want to change the indexing behavior, set the built-in
variable do_fortran_indexing to 1.

jwe