Outputting cell vectors that contain specific information help?

1 Ansicht (letzte 30 Tage)
James Metz
James Metz am 16 Apr. 2020
Kommentiert: Tommy am 17 Apr. 2020
I am trying to formulate a code that takes in input cell array of strings, evaluates the contents of each cell and outputs the cells that contain only uppercase letters
i.e.
cell =
3×2 cell array
{'ABC'} {'Abc'}
{'DEF'} {'dEf'}
{'GHI'} {'ghi'}
ans =
ABC
DEF
GHI
Here's my code so far:
function uppercase = all_caps(cell)
[rows, cols] = size(cell);
for ii = 1:rows
for jj = 1:cols
if contains(cell{ii, jj}, (['a', 'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']) == 0
uppercase = [uppercase, cell{ii,jj}];
end
end
end
end
fprintf('%s\n', uppercase)
end
How can I use MATLABS assortment of built-in functions to prevent me from typing out each individual letter? Additionally, this method doesn't even work.
My professor showed us this solution:
function all_caps(c_in)
[rows, cols] = size(c_in);
for ii = 1:rows
for jj = 1:cols
str = c_in{ii, jj};
if sum(str >= 'A' & str <= 'Z') == length(str)
fprintf('%s\n', str);
end
end
end
end
I am confused about his if statement. I have no clue what the inside is going on inside the sum() function. Please help.
I know these are "two separate questions," but they are about the same function and regard the same topic.

Akzeptierte Antwort

Tommy
Tommy am 16 Apr. 2020
Bearbeitet: Tommy am 16 Apr. 2020
You can use the colon operator to generate a character vector containing each lower case letter in the alphabet:
>> 'a':'z'
ans =
'abcdefghijklmnopqrstuvwxyz'
Same with upper case letters:
>> 'A':'Z'
ans =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Or this, if you want:
>> 'A':'z'
ans =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz'
See here for more info. You can use double() and char() to switch between number and corresponding character:
>> double('a')
ans =
97
>> char(97)
ans =
'a'
So, 'a':'z' is kind of like
>> char(double('a'):double('z'))
ans =
'abcdefghijklmnopqrstuvwxyz'
When you compare characters,
>> 'a' < 'z'
ans =
logical
1
this is like
>> double('a') < double('z')
ans =
logical
1
or rather
>> 97 < 122
ans =
logical
1
In your professor's code:
str >= 'A' & str <= 'Z'
Here, each character within str is being compared to 'A' and 'Z'. The output is a logical array with the same size as str, containing a 1 where the corresponding character of str is between 'A' and 'Z' (i.e. somewhere within the array 'A':'Z').
>> 'GHSIEVBSK' >= 'A' & 'GHSIEVBSK' <= 'Z'
ans =
1×9 logical array
1 1 1 1 1 1 1 1 1
Here, str has all capital letters, so every element in the output is true.
>> 'GhSIeVBsK' >= 'A' & 'GhSIeVBsK' <= 'Z'
ans =
1×9 logical array
1 0 1 1 0 1 1 0 1
Here, some characters in str are lower-case. The corresponding elements in the output are false. With
if sum(str >= 'A' & str <= 'Z') == length(str)
your professor is checking that every element in the output is a 1. Also valid would be
if all(str >= 'A' & str <= 'Z')
Take another look at the documentation for contains.
>> contains('abc', 'a':'z')
ans =
logical
0
This returns 0 because the entirety of 'a':'z' is not found within 'abc'. To check if any element of 'a':'z' is within 'abc', you could use for example
>> contains('abc', cellstr(('a':'z')')')
ans =
logical
1
  3 Kommentare
James Metz
James Metz am 16 Apr. 2020
Bearbeitet: James Metz am 16 Apr. 2020
One question though. What is the point of the additional apostrophies in the cell string?
contains('abc', cellstr(('a':'z')')')
^ ^
Tommy
Tommy am 17 Apr. 2020
Happy to help!
The first apostrophe is the difference between
>> cellstr(('a':'z')')'
ans =
1×26 cell array
Columns 1 through 13
{'a'} {'b'} {'c'} {'d'} {'e'} {'f'} {'g'} {'h'} {'i'} {'j'} {'k'} {'l'} {'m'}
Columns 14 through 26
{'n'} {'o'} {'p'} {'q'} {'r'} {'s'} {'t'} {'u'} {'v'} {'w'} {'x'} {'y'} {'z'}
and
>> cellstr('a':'z')
ans =
1×1 cell array
{'abcdefghijklmnopqrstuvwxyz'}
based on how cellstr works.
Come to think of it, you don't need the second apostrophe.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by