Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: RE: mata: columns of different eltypes in a transmorphic matrix


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: RE: mata: columns of different eltypes in a transmorphic matrix
Date   Thu, 10 Oct 2013 10:06:21 +0100

This is an excellent answer, but one simple point seems submerged. An
elementary strategy is to put numeric variables in a numeric matrix
and string variables in a string matrix.  I've often done that in Mata
programs, and never yet felt the need for structs, pointers, and so
forth.
Nick
njcoxstata@gmail.com


On 10 October 2013 07:59, Joseph Coveney <stajc2@gmail.com> wrote:
> László Sándor wrote:
>
> I would understand the limitation, but then don't understand what
> st_data/st_view matrices will be if the underlying data has both
> string variables and numeric variables. My code did not complain when
> I only had that before.
>
> And in any case, as my implementation is about lookups, indices and
> ordering, it would be a huge pain to collect the data in two matrices
> kept "in sync", meaning the same indices and ordering. Well-well.
>
> --------------------------------------------------------------------------------
>
> Mata's st_data() and st_view() might not have complained, but they
> returned only numeric variables to your matrices; variables containing
> strings were returned as columns of missing real values.  See below.
>
> For what you're doing, you can use -asarray()- for lookups of mixed
> data types.  And you can use pointers.  Or structs.  Consider
> something like
>
> pointer(transmorphic matrix) vector MixedElements
> transmorphic matrix A, B
> A = st_data(., .)
> A = select(A, missing(A[1, .])
> B = st_sdata(., .)
> B = select(B, missing(B[1, .])
> MixedElements = (A, B)
>
> You'd still need to keep track of indices and ordering, but you could
> dump everything into a single Mata thing (a pointer vector of mixed
> matrices . . . or a struct) if that will make keeping things in sync
> easier for you.  When it comes time to use it, choose which element
> type you want, select and dereference the corresponding matrix, and
> then select columns and rows of that at will.  It's probably too
> cumbersome to be doing this kind of thing manually all of the time,
> but for convenience you can define a List class for the same purpose
> that has all of the enabling behavior as methods, with storage
> nitty-gritty kept transparent to the user.
>
> Joseph Coveney
>
> . version 13.0
>
> .
> . clear *
>
> . set more off
>
> . quietly set obs 2
>
> . generate byte a = 1
>
> . generate str1 b = "B"
>
> .
> . mata:
> ------------------------------------------------- mata (type end to
> exit) ---------------------------------------------------------------------
> : mata set matastrict on
>
> :
> : void function test() {
>>         transmorphic matrix A
>>         A = st_data(., .)
>>         printf("A, st_data()\n")
>>         A
>>         eltype(A)
>>
>>         transmorphic matrix B
>>         B = st_sdata(., .)
>>         printf("\n\nB, st_sdata()\n")
>>         B
>>         eltype(B)
>>
>>         st_view(A, ., .)
>>         printf("\n\nA, st_view()\n")
>>         A
>>         eltype(A)
>>
>>         st_sview(B, ., .)
>>         printf("\n\nB, st_sview()\n")
>>         B
>>         eltype(B)
>> }
>
> :
> : test()
> A, st_data()
>        1   2
>     +---------+
>   1 |  1   .  |
>   2 |  1   .  |
>     +---------+
>   real
>
>
> B, st_sdata()
>        1   2
>     +---------+
>   1 |      B  |
>   2 |      B  |
>     +---------+
>   string
>
>
> A, st_view()
>        1   2
>     +---------+
>   1 |  1   .  |
>   2 |  1   .  |
>     +---------+
>   real
>
>
> B, st_sview()
>        1   2
>     +---------+
>   1 |      B  |
>   2 |      B  |
>     +---------+
>   string
>
> :
> : end
> -----------------------------------------------------------------------------------------------------------------------------------------------
>
> .
> end of do-file
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index