I want to sort an array of numbers based on how large they are.
I want to run a loop using the values of an array so that it iterates each value individually. I will try to get it into somekind of psuedo code. I havent programmed in 3 years.
PL=(3,5,7,9,10);
EL=(3,2,2,2,1);
n=input;
x=array;
gp=2.5*(1:n)
% I want this to run for each value of PL seperately
for each PL_i in PL
x=(EL(1,1) < gp <= PL);
% ...and then the vector x subtracted from each val开发者_如何转开发ue of EL
gp2=(x-(EL);
% ...and then put those values from gp2 back into an array
end
Thanks for helping anyone I have worked on this program many hours. This step would greatly aid the entire project.
I can accomplish this using multiple if loops....
g=(gp(gp>0));
gp1=(gp(gp<=EL(1,1)));
if x1>=2
x=(gp((EL(2,1)<gp)));
pp=(gp(gp<=PL(2,1)));
gp2=[x,pp];
gpp2=(x-(EL(2,1)));
lpap=([gp1,gpp2]);
end
if x1>=3
x=(gp((EL(3,1)<gp)));
pp=(gp(gp<=PL(3,1)));
gp2=[x,pp];
gpp3=(x-(EL(3,1)));
lpap=([gp1,gpp2,gpp3]);
end
Alright so I got it, I ended up changing how I wrote the program.
%Number of nodes and elements are entered.
NN=input('Enter the number of elements: ');
if NN <0;
reply = 'Please enter more than 0 for the number of elements: ';
NN=input('Number of Elements?:');
end
% prompt for length of elements also define the global length and total
% length
for x1=1:NN;
EL(x1)=input(['Enter the length of element ', num2str(x1) ': ']);
PL(x1)=tlength;
tlength=tlength+EL(x1);
PL(NN+1)=tlength;
end
%Enter the number of output points
npoints=input('Enter the number of desired output points: ' );
%Establish what order the elements are.
Order=input('Enter the order of the elements (1, 2, 3): ');
%Determine the spacing of the points
spacepoints=tlength/(npoints);
%Determine the global coordinate of the points
gp=(1:spacepoints:PL(NN));
%Get the local coordinates of the points
for c=1:1:npoints;
for i=1:1:NN;
if ((c-1)*spacepoints)<=PL(i+1) && ((c-1)*spacepoints)>=PL(i);
local(c)=((c)*spacepoints)-PL(i);
end
if ((c-1)*spacepoints)>=PL(NN);
local(c)=((c)*spacepoints)-PL(NN);
element(i)=NN;
end
end
end
%Get the natural coordinates from the local coordinates
for c=1:1:npoints;
for i=1:1:NN;
if ((c-1)*spacepoints)<PL(i+1) && ((c)*spacepoints)>PL(i);
nc(c)=((2*local(c))/EL(i))-1;
end
if ((c-1)*spacepoints)>PL(NN);
nc(c)=((2*local(c))/EL(NN))-1;
end
end
end
%Input for the nodal values
for i=1:1:((Order*NN)+1)
values(i)=input(['Enter the nodal value' num2str(i) ': ']);
end
%To find the value for linear
if Order==1
for i=1:1:NN;
for c=1:1:npoints;
if ((c-1)*spacepoints)<=PL(i+1) && ((c)*spacepoints)>=PL(i);
si(c)=1/2*(1-nc(c));
sj(c)=1/2*(1+nc(c));
V(c)=si(c)*values(i)+sj(c)*values(i+1);
end
if ((c-1)*spacepoints)>=PL(NN);
si(c)=1/2*(1-nc(c));
sj(c)=1/2*(1+nc(c));
V(c)=si(c)*values(i)+sj(c)*values(i+1);
end
end
end
end
%To find the value for a quadratic
if Order==2;
for i=1:1:NN;
for c=1:1:npoints;
if ((c-1)*spacepoints)<PL(i+1) && ((c)*spacepoints)>PL(i);
si(c)=(-1/2)*nc(c)*(1-nc(c));
sk(c)=(1/2)*nc(c)*(1+nc(c));
sj(c)=(1-nc(c))*(1+nc(c));
V(c)=si(c)*values(2*i-1)+sj(c)*values(2*i)+sk(c)*values(2*i+1)
end
if ((c-1)*spacepoints)>PL(NN);
si(c)=(-1/2)*nc(c)*(1-nc(c));
sk(c)=(1/2)*nc(c)*(1+nc(c));
sj(c)=(1-nc(c))*(1+nc(c));
V(c)=si(c)*values(i*2-1)+sj(c)*values(i*2)+sk(c)*values((i*2)+1);
end
end
end
end
精彩评论