[R] converting MATLAB -> R | element-wise operation

Richard O'Keefe r@oknz @end|ng |rom gm@||@com
Thu Feb 29 04:15:51 CET 2024


The first vector-oriented programming language I ever learned or used
was APL, and APL makes *no* distinction between row vectors and column
vectors.  It has rank-0 (scalar), rank-1 (vector), rank-2 (matrix),
rank-3 ... and so on arrays.   A rank-1 array is a rank-1 array is a
rank-1 array and there is an algebra of arrays to go with it.   I
learned PL/I around the same time, which also has array expressions
(but at least back then it did not have matrix product).  Again, a 1D
array is a 1D array is a 1D array.  Since 1990, Fortran has been an
array-processing language, and there has never been any distinction
between row vectors and column vectors in Fortran and isn't any such
distinction now.  One thing that these three languages have in common
is the view that a rank-N array and a rank-(N+1) array are not the
same kind of thing.

I can understand MATLAB distinguishing between row and column vectors,
although nothing in my undergraduate numerical analysis education
prepared me to expect or even like it.  But from an APL perspective it
is confusing and limiting.  And for that matter S and R are confusing
in their own way.  But to call R's approach "unique" within the family
of array-oriented programming languages is an exaggeration.  The BASIS
system from Lawrence Livermore does not make any distinction between
row and column vectors; a 1D array is a 1D array is a 1D array.  Most
notably in modern times, Julia belongs to the
a-1D-array-is-a-1D-array-is-a-1D-array and
a-1D-array-is-not-a-2D-array camp.  As one person writing ina Julia
thread put it,
<quote>
Back when I wrote everything in Matlab, those years were characterized
by a series of mild annoyances of always having to worry about whether
a was a row vector or a column vector, and sprinkling a(:) and a(:)'
like pixie dust to fix problems. It’s a huge relief to have real
one-dimensional objects.
</quote>

On Wed, 28 Feb 2024 at 22:43, Berwin A Turlach <berwin.turlach using gmail.com> wrote:
>
> On Tue, 27 Feb 2024 13:51:25 -0800
> Jeff Newmiller via R-help <r-help using r-project.org> wrote:
>
> > The fundamental data type in Matlab is a matrix... they don't have
> > vectors, they have Nx1 matrices and 1xM matrices.
>
> Also known as column vectors and row vectors.  :)
>
> > Vectors don't have any concept of "row" vs. "column".
>
> They do in (numerical) linear algebra.  And MATLAB was written by
> numerical analysts for numerical analysts. :-)  So they distinguish
> between row and column vectors.
>
> GAUSS also distinguishes between row and column vectors.
>
> R (and S) does not distinguish between row and column vectors, and is
> in this aspect quite unique among the groups of vector-oriented
> programming languages (AFAICT).  But  treating every vector as a column
> vector, together with the recycling rule, allows for the easy coding of
> the matrix/vector calculations that one (mostly) comes across in
> statistical computing.  For the rare occasion when this is not true the
> sweep() command is provided, typically remembered once one was bitten
> by the lack of distinction between row and column vectors. :)
>
> Cheers,
>
>         Berwin
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list