1

I want to determine the number of times a character appears in a character array, excluding the time it appears at the last position.

How would I do this?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Francis
  • 33
  • 5

2 Answers2

2

In Matlab computing environment, all variables are arrays, and strings are of type char (character arrays). So your Character Array is actually a string (Or in reality the other way around). Which means you can apply string methods on it to achieve your results. To find total count of occurrence of a character except on last place in a String/Character Array named yourStringVar you can do this:

YourSubString = yourStringVar(1:end-1)
//Now you have substring of main string in variable named YourSubString without the last character because you wanted to ignore it

numberOfOccurrence = length(find(YourSubString=='Character you want to count'))

It has been pointed out by Ray that length(find()) is not a good approach due to various reasons. Alternatively you could do:

numberOfOccurrence = nnz(YourSubString == 'Character you want to count')

numberOfOccurrence will give you your desired result.

Daft Dev
  • 58
  • 1
  • 5
  • `length(find(...))` is very bad - inefficient, slightly obfuscated, and perhaps inelegant. Use `nnz` instead: `nnz(YourSubString == 'character')`. – rayryeng Nov 13 '15 at 07:05
  • @rayryeng I am sorry I am only a Freshman. I will edit the answer and add your suggestion to it. – Daft Dev Nov 13 '15 at 07:08
  • No problem at all :) TBH, I would have used `length(find(...))` many years ago when I first started. Just giving a bit of advice as I now know better. BTW, I see you are a new member. Welcome! – rayryeng Nov 13 '15 at 07:09
  • 1
    Thanks Ray :) Looking forward to contribute and expand my knowledge. – Daft Dev Nov 13 '15 at 07:11
  • Thank you, it really helps. – Francis Nov 13 '15 at 07:35
2

What you can do is map each character into a unique integer ID, then determine the count of each character through histcounts. Use unique to complete the first step. The first output of unique will give you a list of all possible unique characters in your string. If you want to exclude the last time each character occurs in the string, just subtract 1 from the total count. Assuming S is your character array:

%// Get all unique characters and assign them to a unique ID
[unq,~,id] = unique(S); 

%// Count up how many times we see each character and subtract by 1
counts = histcounts(id) - 1;

%// Show table of occurrences with characters
T = table(cellstr(unq(:)), counts.', 'VariableNames', {'Character', 'Counts'});

The last piece of code displays everything in a nice table. We ensure that the unique characters are placed as individual cells in a cell array.

Example:

>> S = 'ABCDABABCDEFFGACEG';

Running the above code, we get:

>> T

T = 

    Character    Counts
    _________    ______

    'A'          3     
    'B'          2     
    'C'          2     
    'D'          1     
    'E'          1     
    'F'          1     
    'G'          1 
rayryeng
  • 102,964
  • 22
  • 184
  • 193