From: Kris zenitis on 27 Apr 2010 17:11 Hello to everybody i ve stuck the the following simple problem and i need help. I have got a simple loop: for i=1:n if dec_values(i)>0 x(i,1)=mod(i,262); y(i,1)=mod(i,442); end end Where the dec_values is a 114000x1 table. I want the x,y to have the calculation of the mod only when i satisfy the statement dec_values(i)>0 . For example when i run this code x finally is a 114000x1 table but the dimension i want is sth like kx1 where k is the sum of how much i satisfy the statement. Any help???
From: Nathan on 27 Apr 2010 17:15 On Apr 27, 2:11 pm, "Kris zenitis" <gio.1...(a)hotmai.com> wrote: > Hello to everybody i ve stuck the the following simple problem and i need help. I have got a simple loop: > > for i=1:n > if dec_values(i)>0 > x(i,1)=mod(i,262); > y(i,1)=mod(i,442); > end > end > > Where the dec_values is a 114000x1 table. I want the x,y to have the calculation of the mod only when i satisfy the statement dec_values(i)>0 . > > For example when i run this code x finally is a 114000x1 table but the dimension i want is sth like kx1 where k is the sum of how much i satisfy the statement. > > Any help??? You will need a separate counting variable then. Let's use k, since you mentioned it. k = 1; for i=1:n if dec_values(i)>0 x(k,1)=mod(i,262); y(k,1)=mod(i,442); k=k+1; end end -Nathan
From: Kris zenitis on 27 Apr 2010 17:27 Nathan thanks a lot for your help but the problem is the same x,y are taking all the i from 1to115000 x=mod(1,262),x=mod(2,262),x=mod(3,262),...,x=mod(115000,262). I dont understand why "if" doesnt work as i wanted.
From: Matt Fig on 27 Apr 2010 17:47 Perhaps you didn't clear out your old x and y? Nathan's solution does what you described. We assume n is the length of dec_values: clear all n = 20; dec_values = round(rand(n,1)*20)-round(rand(n,1)*20); k = 1; for ii=1:n if dec_values(ii)>0 x(k,1)=mod(ii,262); y(k,1)=mod(ii,442); k=k+1; end end % A vectorized approach. idx = find(dec_values>0); x2 = mod(idx,262); y2 = mod(idx,442); isequal(x,x2) isequal(y,y2)
From: Kris zenitis on 27 Apr 2010 18:38
basically ok you have right it works. My problem is that i want x,y to have positive numbers so I include in the for loop one if for i=1:n if dec_values(i)>0 x=mod(i,262); y=mod(i,442); if x>0 & y>0 xx(k,1)=x; yy(k,1)=y; end k=k+1; end But afterthat i still had some zeros in the yy table. |