replacing an element in a matrix

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
Aaron
Posts: 11
Joined: Thu Nov 07, 2013 2:42 pm

replacing an element in a matrix

Unread post by Aaron »

Dear All

Could you look at the following code and resolve the error that I have?


*******
com jjj0 = %seqa(1,1,tm); dis jjj0
com bandw = 7.16326
com jb = jjj0/bandw; dis jb;

com jb0 = jb
do a = 1, %rows(jb)
if %scalar(%xsubmat(jb,a,a,1,1)) <= .5;
com jb0 = %fill(a,1,1)
else com jb0 = %fill(a,1,0)
end do a

dis jb0

*******

Since jb is

0.13960
0.27920
0.41880
0.55841
0.69801
0.83761
0.97721

I expect that the result provides:

1.0000000
1.0000000
1.0000000
0.00000000
0.00000000
0.00000000
0.00000000


But, it gives me

0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000


I would like to replace the element of jb into 1 if the row of jb is less than or equal to 0.5, otherwise 0.
Thank you very much.



Aaron
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: replacing an element in a matrix

Unread post by TomDoan »

Aaron wrote:Dear All

Could you look at the following code and resolve the error that I have?


*******
com jjj0 = %seqa(1,1,tm); dis jjj0
com bandw = 7.16326
com jb = jjj0/bandw; dis jb;

com jb0 = jb
do a = 1, %rows(jb)
if %scalar(%xsubmat(jb,a,a,1,1)) <= .5;
com jb0 = %fill(a,1,1)
else com jb0 = %fill(a,1,0)
end do a

dis jb0

*******

Since jb is

0.13960
0.27920
0.41880
0.55841
0.69801
0.83761
0.97721

I expect that the result provides:

1.0000000
1.0000000
1.0000000
0.00000000
0.00000000
0.00000000
0.00000000


But, it gives me

0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000


I would like to replace the element of jb into 1 if the row of jb is less than or equal to 0.5, otherwise 0.
Thank you very much.



Aaron
Are you trying to copy Gauss or Matlab code? You've now posted three questions where the attempts to program are so un RATS-like that I can't even figure out what you're trying to do. This looks like what you intend. You don't want to use %fill because that would replace the whole matrix.

com jjj0 = %seqa(1,1,tm); dis jjj0
com bandw = 7.16326
com jb = jjj0/bandw; dis jb;

com jb0 = jb
do a = 1, %rows(jb)
compute jb0(a)=%if(jb(a)<=.5,1,0)
end do a

dis jb0

A cleaner way to write this is to use EWISE, which is designed specifically for this type of calculation.

dec vect jb(tm) jb0(tm)
com bandw=7.16326
ewise jb(i)=i/bandw
ewise jb0(i)=(jb(i)<=.5)
Post Reply