Question: From the following code in Matlab, pass it to mathematical calculations with the word UAM and rectify its result with the code already provided clc

From the following code in Matlab, pass it to mathematical calculations with the word "UAM" and rectify its result with the code already provided
clc %clear all variables
clear %clear the screen
aux=[22]; %auxiliary variable that helps us to consider 2 bits of memory
%Initialize counters of the 8 cases to zero (0|00,0|01,0|10, etc...)
cont0_00=0;
cont0_01=0;
cont0_10=0;
cont0_11=0;
cont1_00=0;
cont1_01=0;
cont1_10=0;
cont1_11=0;
%Initialize counters of the 4 cases to zero (00,01,10,11)
cont00=0;
cont01=0;
cont10=0;
cont11=0;
%General counter to 0
conta=0;
% Enter the message
str=input('Enter the message: ');
aux_En=dec2bin(str,8);%convert the message to binary (each character to 8-bit binary)
%separate bit by bit
for t1=1:length(str)
for t2=1:8
En(t1,t2)=bin2dec(aux_En(t1,t2));
end
end
%Concatenate all the bits to have it in a vector
conte=1;
for k1=1:length(En(:,1))
for k2=1:8
P_aux(conte)=En(k1,k2);
conte=conte+1;
end
end
M=P_aux;
for k=1:length(M)
if k==1%For the first iteration, both aux values equal 2, which is of no use to us; they must be 0 or 1
aux(1,1)=2; %first aux value
aux(1,2)=2; %second aux value
else %from the second iteration onwards
aux(1,2)=aux(1,1); %We scan the values in memory, the second value takes the value of the first
aux(1,1)=M(k-1); %The first value of aux takes the first value of the message
end
if k>=3%From the third iteration, these are already favorable cases or events, the values in the memory are already different from 2
conta=conta+1;%the favorable case is counted
if M(k)==0%Cases for when the information bit is 0
if ((aux(1,1)==0) && (aux(1,2)==0))%It asks if the memory has 00
%Increments counters
cont0_00=cont0_00+1;
cont00=cont00+1;
elseif ((aux(1,1)==0) && (aux(1,2)==1))%It asks if the memory has 01
%Increments counters
cont0_01=cont0_01+1;
cont01=cont01+1;
elseif ((aux(1,1)==1) && (aux(1,2)==0))%Asks if the memory has 10
%Increments counters
cont0_10=cont0_10+1;
cont10=cont10+1;
else %Asks if the memory has 11
%Increments counters
cont0_11=cont0_11+1;
cont11=cont11+1;
end
else %Same procedure, for when the information bit is 1
if ((aux(1,1)==0) && (aux(1,2)==0))
cont1_00=cont1_00+1;
cont00=cont00+1;
elseif ((aux(1,1)==0) && (aux(1,2)==1)) cont1_01=cont1_01+1;
cont01=cont01+1;
elseif ((aux(1,1)==1) && (aux(1,2)==0)) cont1_10=cont1_10+1;
cont10=cont10+1;
else cont1_11=cont1_11+1;
cont11=cont11+1;
end
end
end
end
Probabilities are calculated considering favorable cases among total cases
%(P(0|00), P(0|01), P(0|10), etc.
P_0_00=cont0_00/cont;
P_0_01=cont0_01/cont;
P_0_10=cont0_10/cont;
P_0_11=cont0_11/cont;
P_1_00=cont1_00/cont;
P_1_01=cont1_01/cont;
P_1_10=cont1_10/cont;
P_1_11=cont1_11/cont;
Probabilities are calculated considering favorable cases among total cases
%(P(00), P(01), P(10), P(11) P_00=cont00/count;
P_01=cont01/count;
P_10=cont10/count;
P_11=cont11/count;
%Vectors are generated with the probability calculated for the case of 0 and 1 Probas_0=[P_0_00, P_0_01, P_0_10, P_0_11];
Probas_1=[P_1_00, P_1_01, P_1_10, P_1_11];
Test_states=[P_00, P_01, P_10, P_11];
%The denominator is calculated sum=0;
for k=1:length(Probas_0) aux1=Probas_0(k)*Proba_states(k);
aux2=Test_1(k)*Test_states(k);
sum=sum+aux1+aux2;
end %Vectors are generated to calculate the final probabilities Probas=[P_0_00, P_0_01, P_0_10, P_0_11, P_1_00, P_1_01, P_1_10, P_1_11];
States=[P_00, P_01, P_10, P_11, P_00, P_01, P_10, P_11];
%Final probabilities are calculated for k=1:length(Probas) P(k)=(Probas(k)*States(k))/sum;
end %Entropy is calculated for k=1:length(P) if P(k)==0 R(k)=0;
else R(k)=-P(k)*log2(P(k));
end end %Entropy H=sum(R)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!