Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

RE: st: mata for-if-else


From   "Abhimanyu Arora" <abhimanyu.arora@student.kuleuven.be>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: mata for-if-else
Date   Wed, 14 Apr 2010 13:52:04 +0200

Very kind of you for your ever-working answers!
Abhimanyu


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: woensdag 14 april 2010 13:05
To: statalist@hsphsun2.harvard.edu
Subject: RE: st: mata for-if-else

Sorry: you didn't make the second mistake here! 

Nick 
n.j.cox@durham.ac.uk 


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 14 April 2010 11:45
To: statalist@hsphsun2.harvard.edu
Subject: RE: st: mata for-if-else

There are several mistakes here. 
The syntax of -for- requires semi-colons. 
Testing for equality requires == not =. 
Mata has no equivalent of Stata's -if- qualifier. 

Positively, this codes more congenially using ?: 

Try 

for(i=1; i<1001; i++) {
	a[i]  = b[i] == 1 ? 2 : 3 
}

Nick 
n.j.cox@durham.ac.uk 


Abhimanyu Arora

Thanks Sabrina, but unfortunately the problem persists.


If I understood correctly what you meant, I used the following:


mata

a=J(1000,1,1)

for(i=1,i<1001,i++) {
a[i]=2 if b[i]==1
a[i]=3 if b[i]!=1

}

a
end

Sabrina Carrossa

I am not sure, but I guess you'd better use the "if qualifier" instead
of the "if-else" command.

On 14 April 2010 10:54, Abhimanyu Arora

> I have simplified my problem to a large extent, but essentially has to
do
> with using for-if-else commands in mata. I would like to create a
matrix
'a'
> whose values depend on an existing matrix 'b' (both are 1000X1 vectors
to
be
> precise). I need to tell mata the dimension of a first. But all I get
after
> executing the commands below is the original vector 'a'. What could be
the
> possible error? I have tried adding and removing braces but it is not
> working...
>
>
>
>
> a=J(1000,1,.)
> for(i=1,i<1001,i++) {
> if (b[i]==1){
> a[i]=2
> }
> else {a[i]=3
> }
> }
> a
> end
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/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/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index