Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Counting

Subject: Counting

From: Brady

Date: 6 Oct, 2013 13:51:09

Message: 1 of 4

This is very simple, but for some reason I can't figure out the command.
I have a single verticle matrix (32x1) with random values ranging from 8880 to -43 and I need to count every character. I have tried to use the length call, the isspace, and sum, but it always gives me 32. help

Subject: Counting

From: dpb

Date: 6 Oct, 2013 15:05:37

Message: 2 of 4

On 10/6/2013 8:51 AM, Brady wrote:
> This is very simple, but for some reason I can't figure out the command.
> I have a single verticle matrix (32x1) with random values ranging from
> 8880 to -43 and I need to count every character. I have tried to use the
> length call, the isspace, and sum, but it always gives me 32. help

If the data are numeric, there are no "characters" per se, only the
representation to the command window. The number of characters in an
ASCII representation will be dependent on the representation chosen
altho if they are integers(*) you can do something like

 >> x=[-43; 2; 8880];
 >> s=num2str(x);
 >> sum(s(:)~=' ')
ans =
      8
 >>

Eliminate the temporary...

sum(sum(num2str(x)~=' '))

(*) If not integers, then the length of the representation is wholly
dependent on the format chosen of course.

--

Subject: Counting

From: dpb

Date: 6 Oct, 2013 16:41:20

Message: 3 of 4

On 10/6/2013 10:05 AM, dpb wrote:
...

> altho if they are integers(*) you can do something like
>
> >> x=[-43; 2; 8880];
> >> s=num2str(x);
> >> sum(s(:)~=' ')
> ans =
> 8
> >>
>
...

A little cleaner...

 >> length(sprintf('%d',x))
ans =
      8
 >>

--

Subject: Counting

From: Roger Stafford

Date: 6 Oct, 2013 18:06:05

Message: 4 of 4

"Brady" wrote in message <l2rpsd$lq3$1@newscl01ah.mathworks.com>...
> I have a single verticle matrix (32x1) with random values ranging from 8880 to -43 and I need to count every character.
- - - - - - - - - -
  If x is your 32x1 vector and you are sure its values are all integers in the range from -43 to 8880, use matlab's 'histc' function:

 r = (-43:8880)';
 b = histc(x,r);
 t = b~=0;
 B = [r(t),b(t)]'

B will contain the values in r that have non-zero counts in the first column and their corresponding counts in the second column.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us