For loop for equation

Hi
I have a vector of 42*1 values and need to implement two other vectors(l and u) of the same size. The two vectors have a starting value of l(1)=0 and u(1)=1 but need to follow the equation as shown in the image.
My attempt at the code was
l=zeros(size(prob));
u=zeros(size(prob));
u(1,:)=1;
l(1,:)=0;
for i=2:length(u)
for j=2:length(l)
l(i)=l(i-1)+(u(j-1)-l(i-1))*prob(i-1);
u(j)=l(i-1)+(u(j-1)-l(i-1))*prob(j);
end
end
but this did not work. Any help is much appreciated

5 comentarios

Chris
Chris el 14 de Dic. de 2021
Editada: Chris el 14 de Dic. de 2021
I don't think you need two loops--you would only want to count 2:n once.
This code sets i to 2, and runs through j==2:42. Then it moves to i==3 and does j==2:42 again. The image only has n as the single iterator.
Maaz Madha
Maaz Madha el 15 de Dic. de 2021
I tried that as well but it only repeated the first value in the probability vector for every value in both l and u
Chris
Chris el 15 de Dic. de 2021
If you post what you tried, we might be able to find something wrong with it.
Chris
Chris el 15 de Dic. de 2021
Or, eventually someone will come along and answer your homework question for you.
Maaz Madha
Maaz Madha el 15 de Dic. de 2021
F = fopen('Compression.txt','r');
Data=fread(F);%equivalent to str
CharData=char(Data);
disp(Data)
len=length(Data);
C=unique(CharData);
D=unique(Data);
E=[histc(Data,D)];
S=sum(E);
prob=E./S;
u=C;
fprintf('The unique characters are : %s\n',u);
len_unique=length(u);
%% General lookup table
z=zeros(1,len_unique);
cpr=cumsum(p);
newcpr=[0 cpr'];
display(newcpr)
interval=zeros(size(len_unique));
for i=1:len_unique
interval(i,1)=newcpr(i);
interval(i,2)=cpr(i);
end
%% Encoder table
low=0;
high=1;
[tbh,idk]=ismember(D,Data);
%
% pos=idk;
%
% % displaying tag value
% %% Low
%
%
%% Part I'm struggling with
l=zeros(size(prob));
u=zeros(size(prob));
u(1,:)=high;
l(1,:)=low;
for i=2:length(u)
l(i)=l(i-1)+(u(i-1)-l(i-1))*prob(i-1
u(i)=l(i-1)+(u(i-1)-l(i-1))*prob(i);
end
T
The table for my probability is
a
and my vector l and u are showing the same thing
I've even attached my compression text if you want to run the code for yourself.

Iniciar sesión para comentar.

Respuestas (1)

Voss
Voss el 15 de Dic. de 2021

0 votos

l=zeros(size(prob));
u=zeros(size(prob));
u(1,:)=1;
l(1,:)=0;
for i=2:length(u)
l(i)=l(i-1)+(u(i-1)-l(i-1))*prob(i-1);
u(i)=l(i-1)+(u(i-1)-l(i-1))*prob(i);
end

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Preguntada:

el 14 de Dic. de 2021

Comentada:

el 15 de Dic. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by