Page 1 of 1

replacing an element in a matrix

Posted: Wed Jan 22, 2014 11:15 am
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

Re: replacing an element in a matrix

Posted: Wed Jan 22, 2014 12:12 pm
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)