Pre-alocating an array, storing data in them and dynamically using them
Afficher commentaires plus anciens
Hello, I have this code shown below. matrices A, x and C_N are mainly used for data storage which I can acess later on for further calaculations. My problem is this, when t>R, then the code picks the value A(i-1) which is basically the previous value which is then used for further calculations down the code. This only works when the size of the vectors A, x and C_N is equal to 4,1; in this specfic case. That is, when A=zeros(4,1). I realized that this wokrs because when i=4, the condition t>R is satistfied. However, this brings a problem because I dont know in subsequent values of R, at what value of i will the condition t>R be satisfied. For example when the sizes of the mentioned vectors are (10,1) as shown with R=3.303, the code returns matrix Km=[] an empty matrix which implies that k0=0 which is not true, ko should be exactly one. Since A(4-1)=2.331 which whe operated on returns value of 1. How can I solve this? Note that the sizes of A, x and C-N can only be approximated I can know the exact size that will be required. Thank you.
A=zeros(10,1);
x=zeros(10,1);
C_N=zeros(10,1);
R=3.303;
r=1;
x(1)=0;%2*r*sind(acosd((R-r)/(2*r*(fix(R)-1))));
A(1)=R;
C_N(1)=fix(R/r);
for i=2:numel(A)
x(i)=2*r*sind(acosd((A(i-1)-r)/(2*r*(fix(A(i-1)/r)-1))));
A(i)=R*sind(acosd(sum(x)/R));
C_N(i)=2*fix(A(i)/r);
t=sum(x);
if t>R
A(i)=0;
x(i)=0;
C_N(i)=0;
%tr=A(3)
k0=0.5*A(i-1);
k1=k0-fix(k0);
Km=[1:1:fix(0.5*A(i-1))]';
if k1==0
for j=1:numel(Km)
corr(j)=2*fix(2*(sqrt(R^2-(Km(j)*r+0.18)^2)-sum(x))/r);
if j==numel(Km) && corr(j)<1 && numel(Km)==2
corr(j)=fix(2*(sqrt(R^2-(0)^2)-sum(x))/r);
C_Ncorr=sum(corr);
C_N(i-1)=C_Ncorr;
elseif j==numel(Km)
C_Ncorr=sum(corr);
disp( C_Ncorr);
C_N(i-1)=C_Ncorr;
end
end
else
for j=1:numel(Km)
corr(j)=2*fix(2*(sqrt(R^2-(2*Km(j)*r+0.36)^2)-sum(x))/r);
if j==numel(Km)
corrlast=fix(2*(sqrt(R^2-(0)^2)-sum(x))/r);
C_Ncorr=sum(corr)+corrlast;
C_N(i-1)=C_Ncorr;
end
end
end
%C_N(i)=C_Ncorr;
end
% display(C_N(1:i));
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!