Table with number of occurences
Mostra commenti meno recenti
Hi,
I wrote a code that counts the number of occurences and I have to put it in a table with the alphabet as the first column. I can't figure out why the length of my vector is the same as the length of my string, and not as the length of the alphabet. The first 97 elements of vect_A were zeros (why?) so I suppose that this code won't work for a different string.
How can I create that table?
I would really appreciate if someone could correct my code and tell me what is wrong :
Here the code I tried :
s="In the golden lightning Of the sunken sun,Oer which clouds are bright'ning, Thou dost float and run, Like an unbodied joy whose race is just begun";
vect_A=[];
for letter=s(1:length(s));
letter=lower(letter);
for alphabet=['a':'z']
A=count(s,alphabet);
vect_A(alphabet)=A; %stock the value
end
end
B=(vect_A(97:end))'
C=array2table(B)
5 Commenti
Jonas
il 19 Apr 2021
the line
vect_A(alphabet)
is the problem, there you assign e.g. vect_A('a')=5 (i just took any numer). I think what you wanted is vect_A(1)=5, so you have to use numbers as index instead of a char
why that: char(97) equals 'a', so matlab converts your char into a number because matlab needs a number as index
Noa Prasquier
il 19 Apr 2021
Walter Roberson
il 19 Apr 2021
The above suggestion is not correct. It is fine to assign to vect_A(alphabet), since you extract the corresponding portion of the array afterwards. You should not be looping over letter, but in your code doing so only wastes time and does not create an incorrect answer.
Your code calculates B correctly. The only thing you are missing is putting the letters into the table as well.
Noa Prasquier
il 19 Apr 2021
Walter Roberson
il 19 Apr 2021
C.letters = ('a':'z').'
Risposte (0)
Categorie
Scopri di più su Tables in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!