2011-02-15 27 views
0

我想根据它们的大小对数组进行排序。单独评估数组中的点

我想运行一个循环使用数组的值,以便它迭代每个值单独。我会尝试把它变成一些伪代码。我没有编程3年。

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 value of EL 
    gp2=(x-(EL); 
    % ...and then put those values from gp2 back into an array 

end 

感谢您帮助我在这个程序上工作了很多小时的人。这一步将极大地帮助整个项目。

我可以做到这一点使用多个IF循环....

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 
+0

我很困惑你的解释。你能用伪代码写出来吗? – Jonas 2011-02-15 22:44:54

回答

0

好了,所以我得到了它,我最终改变我怎么写的程序。

%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