how can i create a array of letters from a sentence

"Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They're not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or vilify them. About the only thing you can't do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do."
for the above mentioned sentence i would like to obtain the shanon fano code. i tried to count the different entries of charecters.
%Shanon-Fano encoding
src=fopen('ShanonFanotext.txt');%text source
scan=fread(src);%source analysis
symb=unique(scan);%Obtainig info about distinguished charecters
span=length(symb);%No. of symbols used to construct the sentence.
%Obtaining probabilities of each symbols as per the occurences in source
for n=1:length(s3)
d(n,1)=length(find(scan==symb(n)))%frequencies of each symbol
end
Here the fread command just gives the binary output.
i want to carry out for alphabet letters as such a symbol.

5 Comments

Use fileread() instead of fread()
"I tried to count the different entries of charecters." Try this
%%
txt = fileread('ShanonFanotext.txt');
%%
num = double( txt ); % "ascii" numbers
[N,edges] = histcounts( num, min(num):max(num) );
bar( edges, [N,nan] )
There is one hundred spaces in your sample text.
I got this histogram. In the x axis is that asci values?
"In the x axis is that asci values?" Yes
I want the x axis to show the charecters itself
i tried using bar(char(edges),[N,nan]).
Just guessing has its limits:)
I found this code
% Change the labels for the tick marks on the x-axis
irisSpecies = {'Setosa', 'Virginica', 'Versicolor'};
set(gca, 'XTick', 1:3, 'XTickLabel', irisSpecies)
in "Errorbar Plot" at MATLAB Plot Gallery

Sign in to comment.

 Accepted Answer

dpb
dpb on 5 Apr 2020
Edited: dpb on 5 Apr 2020
txt="Here's to ..."; % truncated for brevity of posting but contains above as string variable...
Engine:
t=char(txt).'; % convert string to column char() array
vs=cellstr(unique(t)); % unique characters found in text (variables)
cn=vs; % category names the same ... except
cn(1)={'\b'}; % need a unique name for a blank
c=categorical(t,vs,cn) % and build the categorical array
figure
hHG=histogram(c); % draw nice histogram
hAx=gca;
hAx.XTickLabelRotation=0; % turn labels upright since they're short
nCounts=hHG.Values; % return counts
Easy look-see is
>> summary(c)
\b 100
' 3
, 4
. 14
A 3
B 2
H 1
T 7
Y 1
a 22
b 3
c 12
d 10
e 70
f 9
g 11
h 40
i 17
k 2
l 11
m 10
n 26
o 33
p 5
q 3
r 24
s 29
t 34
u 16
v 2
w 8
y 14
z 3
>>

More Answers (0)

Categories

Asked:

on 5 Apr 2020

Edited:

dpb
on 5 Apr 2020

Community Treasure Hunt

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

Start Hunting!