From: Jan Simon on
Dear Prashant,

Your program fails, if the first number is 0.
I cannot see a connection between the topic thread subject: "While loop goes in infinite", the program and your description.

As far as I understand from your program, you can do this faster:

> st =
> [38 0 0 0 0 0 0 0 0 0 0 18 0 0 0 0
> 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0]

Index = find(st);
ts = [st(Index); diff([Index, length(st)])];
ts = reshape(ts, 1, []);
As in your program leading zeros are ignored.

But I repeat: You did not ask a question or mentioned a problem yet. Can we help you?

Jan
From: Prashant Somani on
@John, @DPB thank you for showing shortest way of doing same, but i require to append zero count after that nozero values only so that is why i had written loop.
Sorry forgot to mention desired answer, it would be like below.
ts =
38 -10 18 -11 17 -15
note that negative signs are only flag to identify that these are count of zero values.

@Jan, the solution you had provided made me very near to my output but it gives one count more for non zero values, can you please guide me in same?
After running code provided by you I found below answer instead of desired above one
ts =
38 11 18 12 17 15
From: dpb on
Prashant Somani wrote:
> @John, @DPB thank you for showing shortest way of doing same, but i
> require to append zero count after that nozero values only so that is
> why i had written loop.
> Sorry forgot to mention desired answer, it would be like below.
> ts =
> 38 -10 18 -11 17 -15 note that negative signs are only flag
> to identify that these are count of zero values.
>
> @Jan, the solution you had provided made me very near to my output but
> it gives one count more for non zero values, can you please guide me in
> same?
> After running code provided by you I found below answer instead of
> desired above one
> ts =
> 38 11 18 12 17 15

Because the difference between nonzero locations (output of diff(find())
is one more than the number of zeros. Note the last value is correct
because Jan used length() to augment the series before doing the diff()
operation.

Fixup...

idx = find(st);
ts = [st(Index); diff([idx, length(st)+1])-1];
ts = reshape(ts, 1, []);

Note you don't need the negative signs to distinguish counts; the
value/counts occur in pairs and can be found that way just as easily.
Or, leave as the 2D array perhaps where second row is count for values
in first.

--
From: Prashant Somani on
@ DPB, thank you very much, requirment fullfilled.