Eric,

I fully agreed with you that anyone doing serious work in various projects such 
as machine learning that make heavy use of mathematical data structures  would 
do well to find some decent well designed and possibly efficient packages to do 
much of the work rather than re-inventing their own way. As you note, it can be 
quite common to do things like flatten a matrix (or higher order array) into a 
vector and do tasks like image recognition using the vector form. 

Mind you, there are other packages that might be a better entry point than 
rTensor and which incorporate that package or similar ones for the user but 
supply other high-level ways to do things like set up neural networks. 

I think the primary focus of this group has been to deal with how to do things 
within a main R distribution and when people start suggesting ways using other 
packages, such as in the tidyverse, that not everyone knows or uses, then there 
is sometimes feedback suggesting a return to more standard topics. Personally, 
I am happy to look at any well-known and appreciated packages even if 
maintained by another company as long as it helps get work done easily. 

I downloaded rTensor just to take a look and see how it implements vec():

> vec
nonstandardGenericFunction for "vec" defined from package "rTensor"

function (tnsr) 
{
    standardGeneric("vec")
}

So, no, they do not directly use the trick of setting the dim attribute and 
that may be because they are not operating on a naked matrix/vector but on an 
enhanced version that wraps it as a tensor. 

I experimented with making a small matrix and calling as.tensor() on it and it 
is quite a bit more complex:

> attributes(mymat)
$dim
[1] 3 4

> attributes(mytens)
$num_modes
[1] 2

$modes
[1] 3 4

$data
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

$class
[1] "Tensor"
attr(,"package")
[1] "rTensor"

This makes sense given that it has to store something more complex. Mind you, 
an R array does it simpler. I am confident this design has advantages for how 
the rTensor package does many other activities. It is more than is needed if 
the OP really has a simple case use.

I do note the vec() function produces the same result as one of the mentioned 
solutions of setting the dim attribute to NULL. 

It would not surprise me if other packages like TensorFlow or ones built on top 
of it like Keras, also have their own ways to do this simple task. The OP may 
want to choose a specific package instead, or as well, for meeting their other 
needs.

-----Original Message-----
From: Eric Berger <ericjber...@gmail.com> 
Sent: Sunday, August 6, 2023 11:59 AM
To: avi.e.gr...@gmail.com
Cc: R-help Mailing List <r-help@r-project.org>
Subject: Re: [R] Stacking matrix columns

Avi,

I was not trying to provide the most economical solution. I was trying
to anticipate that people (either the OP or others searching for how
to stack columns of a matrix) might be motivated by calculations in
multilinear algebra, in which case they might be interested in the
rTensor package.


On Sun, Aug 6, 2023 at 6:16 PM <avi.e.gr...@gmail.com> wrote:
>
> Eric,
>
> I am not sure your solution is particularly economical albeit it works for 
> arbitrary arrays of any dimension, presumably. But it seems to involve 
> converting a matrix to a tensor just to undo it back to a vector. Other 
> solutions offered here, simply manipulate the dim attribute of the data 
> structure.
>
> Of course, the OP may have uses in mind which the package might make easier. 
> We often get fairly specific questions here without the additional context 
> that may help guide a better answer.
>
> -----Original Message-----
> From: R-help <r-help-boun...@r-project.org> On Behalf Of Eric Berger
> Sent: Sunday, August 6, 2023 3:34 AM
> To: Bert Gunter <bgunter.4...@gmail.com>
> Cc: R-help Mailing List <r-help@r-project.org>; Steven Yen <st...@ntu.edu.tw>
> Subject: Re: [R] Stacking matrix columns
>
> Stacking columns of a matrix is a standard operation in multilinear
> algebra, usually written as the operator vec().
> I checked to see if there is an R package that deals with multilinear
> algebra. I found rTensor, which has a function vec().
> So, yet another way to accomplish what you want would be:
>
> > library(rTensor)
> > vec(as.tensor(x))
>
> Eric
>
>
> On Sun, Aug 6, 2023 at 5:05 AM Bert Gunter <bgunter.4...@gmail.com> wrote:
> >
> > Or just dim(x) <- NULL.
> > (as matrices in base R are just vectors with a dim attribute stored in
> > column major order)
> >
> > ergo:
> >
> > > x
> >  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
> > > x<- 1:20  ## a vector
> > > is.matrix(x)
> > [1] FALSE
> > > dim(x) <- c(5,4)
> > > is.matrix(x)
> > [1] TRUE
> > > attributes(x)
> > $dim
> > [1] 5 4
> >
> > > ## in painful and unnecessary detail as dim() should be used instead
> > > attr(x, "dim") <- NULL
> > > is.matrix(x)
> > [1] FALSE
> > > x
> >  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
> >
> > ## well, you get it...
> >
> > -- Bert
> >
> > On Sat, Aug 5, 2023 at 5:21 PM Iris Simmons <ikwsi...@gmail.com> wrote:
> > >
> > > You could also do
> > >
> > > dim(x) <- c(length(x), 1)
> > >
> > > On Sat, Aug 5, 2023, 20:12 Steven Yen <st...@ntu.edu.tw> wrote:
> > >
> > > > I wish to stack columns of a matrix into one column. The following
> > > > matrix command does it. Any other ways? Thanks.
> > > >
> > > >  > x<-matrix(1:20,5,4)
> > > >  > x
> > > >       [,1] [,2] [,3] [,4]
> > > > [1,]    1    6   11   16
> > > > [2,]    2    7   12   17
> > > > [3,]    3    8   13   18
> > > > [4,]    4    9   14   19
> > > > [5,]    5   10   15   20
> > > >
> > > >  > matrix(x,ncol=1)
> > > >        [,1]
> > > >   [1,]    1
> > > >   [2,]    2
> > > >   [3,]    3
> > > >   [4,]    4
> > > >   [5,]    5
> > > >   [6,]    6
> > > >   [7,]    7
> > > >   [8,]    8
> > > >   [9,]    9
> > > > [10,]   10
> > > > [11,]   11
> > > > [12,]   12
> > > > [13,]   13
> > > > [14,]   14
> > > > [15,]   15
> > > > [16,]   16
> > > > [17,]   17
> > > > [18,]   18
> > > > [19,]   19
> > > > [20,]   20
> > > >  >
> > > >
> > > > ______________________________________________
> > > > R-help@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.
> > > >
> > >
> > >         [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help@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.
> >
> > ______________________________________________
> > R-help@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.
>
> ______________________________________________
> R-help@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.
>

______________________________________________
R-help@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.

Reply via email to