COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to count switch statment

• Follow

```hi all
lets say we have
T3=[D1 D2 D3  D4 D5 D6 ];
switch (C)
case (D1)
fprintf( '. A\n' );
case (D2)
fprintf( '. B\n' );
case (D3)
fprintf( '. C \n' );
case (D4)
fprintf( '. D \n' );
End
if we  call this code by matlab function many times.. how can we count
how many time it will print out A and how many time it will print B
and so on??
```
 0

```Gumah wrote:

> lets say we have
> T3=[D1 D2 D3  D4 D5 D6 ];
> switch (C)
>     case (D1)
>        fprintf( '. A\n' );
>     case (D2)
>         fprintf( '. B\n' );
>     case (D3)
>         fprintf( '. C \n' );
>     case (D4)
>             fprintf( '. D \n' );
> End
> if we  call this code by matlab function many times.. how can we count
> how many time it will print out A and how many time it will print B
> and so on??

If Cvec is a vector containing all of the different C that the function
will be invoked with, then

Counts = histc(CVec, sort(T3));

Note, though, that Counts will be in sorted order of T3, not in the
original order. Study the sort() command to figure out how to get back
to the original order.
```
 0

```On May 21, 12:23=A0pm, Walter Roberson <rober...@hushmail.com> wrote:
> Gumah wrote:
> > lets say we have
> > T3=3D[D1 D2 D3 =A0D4 D5 D6 ];
> > switch (C)
> > =A0 =A0 case (D1)
> > =A0 =A0 =A0 =A0fprintf( '. A\n' );
> > =A0 =A0 case (D2)
> > =A0 =A0 =A0 =A0 fprintf( '. B\n' );
> > =A0 =A0 case (D3)
> > =A0 =A0 =A0 =A0 fprintf( '. C \n' );
> > =A0 =A0 case (D4)
> > =A0 =A0 =A0 =A0 =A0 =A0 fprintf( '. D \n' );
> > End
> > if we =A0call this code by matlab function many times.. how can we coun=
t
> > how many time it will print out A and how many time it will print B
> > and so on??
>
> If Cvec is a vector containing all of the different C that the function
> will be invoked with, then
>
> Counts =3D histc(CVec, sort(T3));
>
> Note, though, that Counts will be in sorted order of T3, not in the
> original order. Study the sort() command to figure out how to get back
> to the original order.- Hide quoted text -
>
> - Show quoted text -
This code will find the minimum of T3.. the minimum =3DD1 it will print
out A.. my question.. if this code was called by a function many
times.. each time D1,D2,... are different.. so the minimum will be
different each time.. so it will print out different letter.. How can
accout how many times it will print out A and how many times it will
print out B and so on

T3=3D[D1 D2 D3  D4 D5 D6 ];
C=3Dmin(T);
switch (C)
case (D1)
fprintf( '. A\n' );
case (D2)
fprintf( '. B\n' );
case (D3)
fprintf( '. C \n' );
case (D4)
fprintf( '. D \n' );
End
```
 0

```Gumah wrote:

> This code will find the minimum of T3.. the minimum =D1 it will print
> out A.. my question.. if this code was called by a function many
> times.. each time D1,D2,... are different.. so the minimum will be
> different each time.. so it will print out different letter.. How can
> accout how many times it will print out A and how many times it will
> print out B and so on
>
> T3=[D1 D2 D3  D4 D5 D6 ];
> C=min(T);
> switch (C)
>     case (D1)
>        fprintf( '. A\n' );
>     case (D2)
>         fprintf( '. B\n' );
>     case (D3)
>         fprintf( '. C \n' );
>     case (D4)
>             fprintf( '. D \n' );
> End

Are you trying to figure out how many times it _would_ print out those
letters, but without executing the code and counting? Or are you trying
to change the code so that it keeps track of the number of times each
one has been printed, and then access those counts at the end?

If you are trying to count the number of times actually printed, you
will either need to make the function a nested function, or you will
need to use a 'global' variable.

If you are trying to predict the number of times it _would_ print, but
without actually executing the code, then you need to be clearer as to
what information is available. For example, if it is to be called N
times, then do you have an N x 6 array of all of the values it would be
called with?
```
 0

```On May 21, 1:37=A0pm, Walter Roberson <rober...@hushmail.com> wrote:
> Gumah wrote:
> > This code will find the minimum of T3.. the minimum =3DD1 it will print
> > out A.. my question.. if this code was called by a function many
> > times.. each time D1,D2,... are different.. so the minimum will be
> > different each time.. so it will print out different letter.. How can
> > accout how many times it will print out A and how many times it will
> > print out B and so on
>
> > T3=3D[D1 D2 D3 =A0D4 D5 D6 ];
> > C=3Dmin(T);
> > switch (C)
> > =A0 =A0 case (D1)
> > =A0 =A0 =A0 =A0fprintf( '. A\n' );
> > =A0 =A0 case (D2)
> > =A0 =A0 =A0 =A0 fprintf( '. B\n' );
> > =A0 =A0 case (D3)
> > =A0 =A0 =A0 =A0 fprintf( '. C \n' );
> > =A0 =A0 case (D4)
> > =A0 =A0 =A0 =A0 =A0 =A0 fprintf( '. D \n' );
> > End
>
> Are you trying to figure out how many times it _would_ print out those
> letters, but without executing the code and counting? Or are you trying
> to change the code so that it keeps track of the number of times each
> one has been printed, and then access those counts at the end?
>
> If you are trying to count the number of times actually printed, you
> will either need to make the function a nested function, or you will
> need to use a 'global' variable.
>
> If you are trying to predict the number of times it _would_ print, but
> without actually executing the code, then you need to be clearer as to
> what information is available. For example, if it is to be called N
> times, then do you have an N x 6 array of all of the values it would be
> called with?- Hide quoted text -
>
> - Show quoted text -

I am trying to change the code so that it keeps track of the number of
times each
one has been printed, and then access those counts at the end..
```
 0

```Gumah wrote:
> On May 21, 1:37 pm, Walter Roberson <rober...@hushmail.com> wrote:
>> Gumah wrote:

>>> T3=[D1 D2 D3  D4 D5 D6 ];
>>> C=min(T);
>>> switch (C)
>>>     case (D1)
>>>        fprintf( '. A\n' );
>>>     case (D2)
>>>         fprintf( '. B\n' );
>>>     case (D3)
>>>         fprintf( '. C \n' );
>>>     case (D4)
>>>             fprintf( '. D \n' );
>>> End

>> If you are trying to count the number of times actually printed, you
>> will either need to make the function a nested function, or you will
>> need to use a 'global' variable.

> I am trying to change the code so that it keeps track of the number of
> times each
> one has been printed, and then access those counts at the end..

Well, re-read my hint, re-quoted above.
```
 0

5 Replies
235 Views

Similiar Articles:

7/12/2012 9:38:41 PM