how to call a variable inside a function

1 visualización (últimos 30 días)
PRABAKARAN ARUMUGAM
PRABAKARAN ARUMUGAM el 9 de Feb. de 2020
Editada: Stephen23 el 10 de Feb. de 2020
sname=textread('USnames.txt', '%s');
for alp=["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"];
containsAZ(:,1) = contains(sname,"(alp)", 'IgnoreCase', true )
end
Hi, here sname has all USA state names
I am going to compare alphabets, my code is comparing the letters alp and giving locial output. Instead I need, the first iteration of a loop should be "A"
and second should be B, like so on upto Z. how to write a loop for that.
Thanks in advance

Respuestas (2)

Image Analyst
Image Analyst el 10 de Feb. de 2020
Is this different than your post under your other account: Click here
The code below works to count the letters in each state name:
stateNames = {...
'Alabama'
'Alaska'
'Arizona'
'Arkansas'
'California'
'Colorado'
'Connecticut'
'Delaware'
'Florida'
'Georgia'
'Hawaii'
'Idaho'
'Illinois'
'Indiana'
'Iowa'
'Kansas'
'Kentucky'
'Louisiana'
'Maine'
'Maryland'
'Massachusetts'
'Michigan'
'Minnesota'
'Mississippi'
'Missouri'
'Montana'
'Nebraska'
'Nevada'
'New Hampshire'
'New Jersey'
'New Mexico'
'New York'
'North Carolina'
'North Dakota'
'Ohio'
'Oklahoma'
'Oregon'
'Pennsylvania'
'Rhode Island'
'South Carolina'
'South Dakota'
'Tennessee'
'Texas'
'Utah'
'Vermont'
'Virginia'
'Washington'
'West Virginia'
'Wisconsin'
'Wyoming'}
numStates = length(stateNames)
containsAZ = zeros(numStates, 26);
letters = 'A' : 'Z'
for k = 1 : numStates
% Get the upper case version of this state.
thisState = upper(stateNames{k});
% Count how many times that letter occurs in it.
for c = 1 : length(letters)
thisLetter = letters(c);
containsAZ(k, c) = sum(thisState == thisLetter);
end
% For fun, print out how many characters are in the state name.
for col = 1 : length(letters)
thisCount = containsAZ(k, col);
if thisCount > 0
fprintf('%s contains %d instances of the letter "%c".\n', ...
thisState, thisCount, letters(col));
end
end
end
% Show in command window the counts for each letter.
% Each row is a state name.
containsAZ
% If you want a binary matrix like true or false for whether that letter
% is in the name, rather than a count, just threshold it:
logicalContainsAZ = containsAZ > 0

Stephen23
Stephen23 el 10 de Feb. de 2020
Editada: Stephen23 el 10 de Feb. de 2020
No need for a loop:
nAZ = histc(char(lower(stateNames)).','a':'z').';
E.g.:
>> nAZ = histc(char(lower(stateNames)).','a':'z').';
>> cAZ = ['a':'z';char(nAZ+'0')];
>> cAZ(:,end+1) = ' ';
>> [cAZ,char('State Name',stateNames{:})]
ans =
abcdefghijklmnopqrstuvwxyz State Name
41000000000110000000000000 Alabama
30000000001100000010000000 Alaska
20000000100001100100000001 Arizona
30000000001001000120000000 Arkansas
20100100200101100100000000 California
10110000000100300100000000 Colorado
00301000100002100002100000 Connecticut
20012000000100000100001000 Delaware
10010100100100100100000000 Florida
10001020100000100100000000 Georgia
20000001200000000000001000 Hawaii
10010001100000100000000000 Idaho
00000000300201100010000000 Illinois
20010000200002000000000000 Indiana
10000000100000100000001000 Iowa
20000000001001000020000000 Kansas
00101000002001000001100010 Kentucky
20000000200101100010100000 Louisiana
10001000100011000000000000 Maine
20010000000111000100000010 Maryland
20101001000010000042100000 Massachusetts
10100011200011000000000000 Michigan
10001000100012100011000000 Minnesota
00000000400010020040000000 Mississippi
00000000200010100120100000 Missouri
20000000000012100001000000 Montana
21001000001001000110000000 Nebraska
20011000000001000000010000 Nevada
10002002100011010110001000 New Hampshire
00003000010001000110001010 New Jersey
00102000100011100000001100 New Mexico
00001000001001100100001010 New York
20100001100102200201000000 North Carolina
20010001001001200102000000 North Dakota
00000001100000200000000000 Ohio
20000001001110200000000000 Oklahoma
00001010000001200100000000 Oregon
20001000100103010010010010 Pennsylvania
10021001100101100110000000 Rhode Island
20100001100101200111100000 South Carolina
20010001001000200012100000 South Dakota
00004000000002000021000000 Tennessee
10001000000000000011000100 Texas
10000001000000000001100000 Utah
00001000000011100101010000 Vermont
10000010300001000100010000 Virginia
10000011100002100011001000 Washington
10001010300001000111011000 West Virginia
00100000200002100020001000 Wisconsin
00000010100011100000001010 Wyoming

Categorías

Más información sobre Particle & Nuclear Physics en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by