Prev: NEED MATLAB PROGRAMMER FOR BUILDING OPTIONS PORTFOLIO
Next: Editing figure data points (not manually)
From: Prashant Somani on 24 Jul 2010 07:53 I have tried following code for array st as mentioned Array: st = Columns 1 through 16 38 0 0 0 0 0 0 0 0 0 0 18 0 0 0 0 Columns 17 through 32 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 Columns 33 through 39 0 0 0 0 0 0 0 Code: while i<=size(st,2) if st(i)~=0 ts(k)=st(i); k=k+1; c=0; i=i+1; else while st(i)==0 c=c-1; if i<38 i=i+1; else break end end ts(k)=c; k=k+1; end end What i want to do is sotre nonzero values as itis and count zero and put its value in same array.
From: John D'Errico on 24 Jul 2010 09:19 "Prashant Somani" <p.m.somani(a)gmail.com> wrote in message <i2ek6u$ohq$1(a)fred.mathworks.com>... > I have tried following code for array st as mentioned > > Array: > st = > > Columns 1 through 16 > > 38 0 0 0 0 0 0 0 0 0 0 18 0 0 0 0 > > Columns 17 through 32 > > 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 > > Columns 33 through 39 > > 0 0 0 0 0 0 0 > > > Code: > while i<=size(st,2) > if st(i)~=0 > ts(k)=st(i); > k=k+1; > c=0; > i=i+1; > else > while st(i)==0 > c=c-1; > if i<38 > i=i+1; > else > break > end > end > ts(k)=c; > k=k+1; > end > end > > What i want to do is sotre nonzero values as itis and count zero and put its value in same array. shiver .... Long, convoluted code that I'm not going to bother to follow and apparently fails to work anyway, all to do something trivial. % store the non-zero elements of st ts = st(st ~= 0); If you wish to count the number of zeros, you might do something like this: count = numel(st) - numel(ts); Or to do it directly, do this: count = sum(st == 0); Learn to use the capabilities of matlab. Use vector operations that operate on entire arrays, not loops. John
From: Jan Simon on 24 Jul 2010 09:23 Dear Prashant, What is your question? Any errors? Does the result differ from the expectations? Is it slow? Kind regards, Jan
From: dpb on 24 Jul 2010 09:20 Prashant Somani wrote: > I have tried following code for array st as mentioned > > Array: > st = > > Columns 1 through 16 > 38 0 0 0 0 0 0 0 0 0 0 18 0 0 0 0 > Columns 17 through 32 > 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 > Columns 33 through 39 > 0 0 0 0 0 0 0 > ....[code elided for brevity]... > What i want to do is sotre nonzero values as itis and count zero and put > its value in same array. I'm not completely sure what you mean by "put its value in same array" but the two pieces of information are ts = st(st~=0); % save nonzero values in ts nz = sum(st==0); % count number zeros Alternatively, nz = length(st)-length(ts); If the last part is that you want to append the count to the list of nonzero values then ts = [ts nz]; --
From: dpb on 24 Jul 2010 17:08 dpb wrote: .... > ts = st(st~=0); % save nonzero values in ts .... Of course, if don't need it for something else, if the point is to retain the nonzero values of st, then st(st==0) = []; is yet another cat... --
|
Next
|
Last
Pages: 1 2 Prev: NEED MATLAB PROGRAMMER FOR BUILDING OPTIONS PORTFOLIO Next: Editing figure data points (not manually) |