Pre-alocating an array, storing data in them and dynamically using them
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
okoth ochola
am 28 Jul. 2025
Beantwortet: Stephen23
am 28 Jul. 2025
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
0 Kommentare
Akzeptierte Antwort
Stephen23
am 28 Jul. 2025
I think the problem is that you're checking the condition t > R at a fixed position in your loop, but you don't know beforehand which iteration i will satisfy this condition when you change the vector sizes or the value of R.
The solution is to break out of the loop as soon as the condition t > R is met, rather than continuing to iterate through all elements. Here's a modified version of your code:
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;
% Use A(i-1) which contains the previous valid value
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
% Break out of the main loop once condition is met
break;
end
% display(C_N(1:i));
end
% Display results
fprintf('Final k0 value: %.3f\n', k0);
fprintf('Km matrix:\n');
disp(Km);
fprintf('Valid entries in A: %d\n', sum(A~=0));
fprintf('A values:\n');
disp(A(A~=0));
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Performance finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!