hello , i use the following code , the loop is running to (o=30) . More than 30 value does not work and display the Warning (Index exceeds matrix dimensions) .. What correct it in order to complete iteration?

1 view (last 30 days)
s=4;
z0=0.3;
index=7;index1=0;
o=40;
a=normrnd(0,s^2,5000,1);
phi=0.5;
n=[300;350;400;480;500;600;650;700;800;900;1000;1200;1300;1500;1700;1900;2000;2100;2300;2500;2700;2900;3000;3300;3500;3900;4200;4400;4600;4900;5300;5700;5900;6000;6600;6800;7000;7100;7300;7500;7900;8200;8500;8700;8900;9000;9200;9500;9700;1000];
for nn=1:o
z=[];
z(1)=phi*z0+a(1);
for i=2:n(nn)
z(i)=phi*z(i-1)+a(i);
end
for i=2:n(nn)
g(i)=z(i)*z(i-1);
end
k1=sum(g,2);
num= (n(nn)-2)*k1;
for i=3:n(nn)
f(i)=z(i-1)*z(i-1);
end
p=sum(f,2);
den=(n(nn)-1)*p;
d=min(z);
h=max(z);
k =7;
L= (h-d)/k;
intK = round(k);
out=[linspace(d,h-L,intK)',linspace(d+L,h,intK)'];
for i=1:n(nn)
for j=1:k
if (out(j,1)<=z(i)&z(i)<=out(j,2))
A(j,1,i)=z(i);
end
end
end
for i=1:k
temp1=A(i,:,:);
temp2 = nonzeros(temp1);
temp3 = length(temp2);
le(i)=temp3;
end
for i=1:k
if(le(i)==max(le))
l=(out(i,2)-out(i,1))/4;
index=length(out)+1;
out(index,:)=[out(i,1) out(i,1)+l];
out(index+1,:)=[out(i,1)+l out(i,1)+2*l];
out(index+2,:)=[out(i,1)+2*l out(i,1)+3*l];
out(index+3,:)=[out(i,1)+3*l out(i,1)+4*l];
out(i,:)=[0 0];
end
end
t=max(le);
for i=1:k
if(le(i)==t)
le(i)=0.5;
end
end
for i=1:k
if(le(i)==max(le))
l=(out(i,2)-out(i,1))/3;
index=length(out)+1;
out(index,:)=[out(i,1) out(i,1)+l];
out(index+1,:)=[out(i,1)+l out(i,1)+2*l];
out(index+2,:)=[out(i,1)+2*l out(i,1)+3*l];
out(i,:)=[0 0];
end
end
v=max(le);
for i=1:k
if(le(i)==v)
le(i)=0.5;
end
end
for i=1:k
if(le(i)==max(le))
l=(out(i,2)-out(i,1))/2;
index=length(out)+1;
out(index,:)=[out(i,1) out(i,1)+l];
out(index+1,:)=[out(i,1)+l out(i,1)+2*l];
out(i,:)=[0 0];
end
end
for i=1:k
if(le(i)==0)
out(i,:)=[0 0];
end
end
format long g
out;
out( all(~out,2), : ) = [];
u=[sort(out(:,1)) sort(out(:,2))];
for i=1:length(u)
mid(i)=(u(i,1)+u(i,2))/2;
q(i)=(u(i,2)-u(i,1))/4;
down(i)=u(i,1)+q(i);
up(i)=u(i,1)+3*q(i);
len(i)=u(i,2)-u(i,1);
end
zz=[];
for j=1:length(u)
if(u(j,1)<=z(2)&z(2)<=u(j,2))
zz(2)=mid(j);
end
end
for j=1:length(u)
if(u(j,1)<=z(3)&z(3)<=u(j,2))
mo=abs(z(2)-z(1));
if((mo)>len(j))
zz(3)=up(j);
elseif ((mo)==len(j))
zz(3)=mid(j);
elseif ((mo)<len(j))
zz(3)=down(j);
end
end
end
for r=3:(n(nn)-1)
for i=1:length(u)
if (u(i,1)<=z(r)&z(r)<=u(i,2))
for j=1:length(u)
if (u(j,1)<=z(r+1)&z(r+1)<=u(j,2))
if (j>i)
mo=abs((z(r)-z(r-1))-(z(r-1)-z(r-2)));
if (((z(r)-z(r-1))-(z(r-1)-z(r-2)))>0)
if((u(j,1)<=(mo*2+z(r))&(mo*2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo*2)&(z(r)-mo*2)<=u(j,2)))
zz(r+1)=up(j);
elseif ((u(j,1)<=(mo/2+z(r))&(mo/2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo/2)&(z(r)-mo/2)<=u(j,2)))
zz(r+1)=down(j);
else
zz(r+1)=mid(j);
end
elseif (((z(r)-z(r-1))-(z(r-1)-z(r-2)))<0)
if ((u(j,1)<=(mo/2+z(r))&(mo/2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo/2)&(z(r)-mo/2)<=u(j,2)))
zz(r+1)=down(j);
elseif ((u(j,1)<=(mo*2+z(r))&(mo*2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo*2)&(z(r)-mo*2)<=u(j,2)))
zz(r+1)=up(j);
else
zz(r+1)=mid(j);
end
end
elseif (j<i)
mo=abs((z(r)-z(r-1))-(z(r-1)-z(r-2)));
if (((z(r)-z(r-1))-(z(r-1)-z(r-2)))>0)
if((u(j,1)<=(mo*2+z(r))&(mo*2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo*2)&(z(r)-mo*2)<=u(j,2)))
zz(r+1)=up(j);
elseif ((u(j,1)<=(mo/2+z(r))&(mo/2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo/2)&(z(r)-mo/2)<=u(j,2)))
zz(r+1)=down(j);
else
zz(r+1)=mid(j);
end
elseif (((z(r)-z(r-1))-(z(r-1)-z(r-2)))<0)
if ((u(j,1)<=(mo/2+z(r))&(mo/2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo/2)&(z(r)-mo/2)<=u(j,2)))
zz(r+1)=down(j);
elseif ((u(j,1)<=(mo*2+z(r))&(mo*2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo*2)&(z(r)-mo*2)<=u(j,2)))
zz(r+1)=up(j);
else
zz(r+1)=mid(j);
end
end
else
mo=abs((z(r)-z(r-1))-(z(r-1)-z(r-2)));
if (((z(r)-z(r-1))-(z(r-1)-z(r-2)))>0)
if((u(j,1)<=(mo*2+z(r))&(mo*2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo*2)&(z(r)-mo*2)<=u(j,2)))
zz(r+1)=up(j);
elseif ((u(j,1)<=(mo/2+z(r))&(mo/2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo/2)&(z(r)-mo/2)<=u(j,2)))
zz(r+1)=down(j);
else
zz(r+1)=mid(j);
end
elseif (((z(r)-z(r-1))-(z(r-1)-z(r-2)))<0)
if ((u(j,1)<=(mo/2+z(r))&(mo/2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo/2)&(z(r)-mo/2)<=u(j,2)))
zz(r+1)=down(j);
elseif ((u(j,1)<=(mo*2+z(r))&(mo*2+z(r))<=u(j,2))|(u(j,1)<=(z(r)-mo*2)&(z(r)-mo*2)<=u(j,2)))
zz(r+1)=up(j);
else
zz(r+1)=mid(j);
end
end
end
end
end
end
end
end
zz(1)=z(1);
for i=2:n(nn)
g2(i)=zz(i)*zz(i-1);
end
k2=sum(g2,2);
num2= (n(nn)-2)*k2;
for i=3:n(nn)
f2(i)=zz(i-1)*zz(i-1);
end
p2=sum(f2,2);
den2=(n(nn)-1)*p2;
index1=index1+1;
ma(index1)=n(nn);
phihat(index1)=num/den;
phihat2(index1)=num2/den2;
end
for i=1:(o)
table(i,:)=[ma(i) phi phihat(i) phihat2(i)];
end
format long g
table
for i=1:(nn)
da(i)=(phihat(i)-phi)^2;
db(i)=(phihat2(i)-phi)^2;
end
asd=sum (da)/(o);
asf=sum (db)/(o);
format long g
asd
asf

Accepted Answer

John D'Errico
John D'Errico on 30 Aug 2016
Edited: John D'Errico on 30 Aug 2016
Shiver. Obscene code. Not a single comment. Variables that have no apparent meaning to anyone, even the author. All to compute something where we are given no clue as to the purpose. Using the letter o as a variable name is a bad idea too, as it is too easily confused with the number 0 in some fonts. This will one day cause bugs for you, when you type 0 meaning o, or vice versa.
You do not pay extra per character. Variables with actual mnemonic names are not less efficient to use. But they do allow you to read the code more easily. This helps you to debug the code. Comments help you to debug the code.
Learn to use indentation constructively. This helps you to visualize loops and if statements.
Next, learn to use functions. They help to break the code into small fragments, each of which is then easy to debug. Test those functions separately. Verify every fragment does what you want it to do.
No, none of this helps you to solve your problem. At the same time, your problem is impossible to diagnose by someone else. We cannot read this mess of spaghetti code, nor can we know what it SHOULD be doing. If there is a bug (very possible) then how do we know where the bug came from?
For example, in the very beginning, you have a call to normrnd. In the help for normrnd, the second argument is a standard deviation.
a=normrnd(0,s^2,5000,1); % A probable error
Note that you are passing in the square of a variable here, a probable variance. You won't get what you expect to see. Is this your problem? Who knows? It is most likely just a bug in your code that will probably generate meaningless garbage for results. But how can I know? Perhaps you have defined the variable s as the sqrt of a standard deviation?
I did enjoy this code fragment:
format long g
out;
Of course, nothing will be displayed.
So I could continue to look through your code, trying to get inside your head. That would take god knows how long to do. So the only person who can debug this code is you. Learn to use tools like the debugger.
You should learn to write cleaner, readable, documented code. If it takes you a few minutes longer to write, who cares? It may well take you hours to debug this mess of code. For anyone else, that task would take far longer, even if they are given a clue as to what it should be doing.
This is a script. So learn to use scripting tools. Add comments. Learn to use the editor to run a section of your scripts, broken up by lines that start with the %% characters. Then you can run a small block of code, verify that it ran correctly. You can then fix any problems, and then re-run that block.
%%initialization of constants
s=4; % standard deviation of something for a simulation
z0=0.3;
index=7;index1=0;
o=40;
% what will a be used for? why 5000 elements?
a=normrnd(0,s^2,5000,1); % A probable error
phi=0.5;
% what is n?
n=[300;350;400;480;500;600;650;700;800;900;1000;1200;1300;1500;1700;1900;2000;2100;2300;2500;2700;2900;3000;3300;3500;3900;4200;4400;4600;4900;5300;5700;5900;6000;6600;6800;7000;7100;7300;7500;7900;8200;8500;8700;8900;9000;9200;9500;9700;1000];
%%What do these loops do?
for nn=1:o
z=[];
z(1)=phi*z0+a(1);
for i=2:n(nn)
z(i)=phi*z(i-1)+a(i);
end
for i=2:n(nn)
g(i)=z(i)*z(i-1);
end
...
There are LOTS of other problems I could point out. For example, growing the variable z dynamically is REALLY bad idea. It will create very slow running code. You know how big it will be in the end!!!!!!! So preallocate z to be the final size. Otherwise, you force MATLAB to reallocate more and more memory every time the array is grown, copying the entire array to new locations. So this operation takes quadratic time to process, growing slower and slower, quadratically with size of the array z.
  2 Comments
John D'Errico
John D'Errico on 30 Aug 2016
Edited: John D'Errico on 30 Aug 2016
Many years ago, I was given (in my job) come code to maintain. Some hundreds of lines of dense fortran code. Only one single comment line. It said (just before a matrix named C was built)
% compute C matrix here
Nothing else. Nothing really to be done except toss the code in the bit bucket, and re-write it from scratch. The author had left the company, leaving behind a legacy of useless code. Impossible to repair if there was a problem. Impossible to read, impossible to understand should someone ever want to use their code.
Is that the legacy you want to leave behind?

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!