Output to an Array

조회 수: 10 (최근 30일)
Lily Muller
Lily Muller 2014년 9월 4일
답변: Iain 2014년 9월 4일
I am trying to input variables into the function gpoly which is designed to calculate the gravity (g) for a 2D rectangle defined by the X-co-ordinates (xcorn) and Z-co-ordinates (zcorn). I have written a for loop to do this over a series of rectangles adjacent to each other and the output should be the gravity (g) for each rectangle. There should be 100 rectangles. However the output array is 2001x1 and has only the value from the final rectangle which is repeated every 20th value of the array when it should be 100x1 with the g values for each of the rectangles. This process is repeated for different values of dz - this should produce a new array, though I'm not sure how to do this. Please help me with the outputs as I think I'm hopelessly wrong!
clear
cellcnt=100
xmax=2001
xmin=1
dx=(xmax-xmin)/cellcnt
z=30
g=zeros(1,100);
for dz=10:10:100,
for x=xmin:dx:xmax,
xcorn=[x, x+dx, x+dx, x];
zcorn=[z, z, z+dz, z+dz];
ncorn=4;
x0=x+(dx/2);
z0=0;
rho=3230; %kg/m3
g(x)=gpoly(x0,z0,xcorn,zcorn,ncorn,rho);
end
end
Thanks for the help.
Lily

답변 (1개)

Iain
Iain 2014년 9월 4일
You're indexing like this:
for x = 1:20:2001
g(x) = blah;
end
You should be indexing like this:
for i = 1:1:100
x = x + dx;
g(i) = blah;
end
I'm not sure if you have another problem in the code - me no know what gpoly does!

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by