MATLAB: Interpolating from different rows in a for loop

cross-sectionfor loopindexinginterp1interpolationlinspace

Hello!
I want to interpolate between multiple cross-sections. Each cross-section is a 10×100 matrix of XY coordinates. Each cross-section is represented by a single X value with variation along the Y. The number of Y values per cross-section is equal. I want to interpolate the Y values along a linearly spaced vector to their analogue in the neighbouring cross-section. After this is done for one pair of cross-sections (say X1 and X2) it will move onto onto the next pair (in this case, X2 and X3.)
For example, if X1 is the first cross-section, and X2 the second, and each cross-section has 100 linearly spaced Y values, then Y1 in X1 will be interpolated along a linearly spaced vector to their analogue Y2 in X2. This repeats for Y2, Y3, Y4…Y100. Then the next pair of cross-sections, X2 and X3, is treated.
I can get my code to work for one iteration but after that it stops. I get the error "index exceeds matrix dimensions". I think this stems from how I define the points on the second cross-section e.g.
X((i+1),n)
I couldn't find anything on how to reference the relevant value the next row down in a matrix from within a for loop however. Would appreciate any advice!
For an idea, I've attached some sample data for X and Y. The image attached is similar to the final output I want: all points connected linearly between cross-sections. However rather than the first line shown, I want it for all rows of Y. Currently it only does on row.
Many thanks.
Full code below:
%loop for per cross-section
n = length(X(:,1));
for i = 1:n;
ns = length(Y(i,:));
%loop for each Y per cross-section
for j = 1:ns
%specify given data for interpolation between cross-sections
Yi= [Y(i,n),Y((i+1),n)];
Xi= [X(i,n),X((i+1),n)];
%specify data to generate points between
Xa = X(i,n);
Xb= X((i+1),n);
%linearly spaced vectors between cross-section X
xvi=linspace(Xa,Xb,100);
%interpolate Y along new X cordinates
Y=[interp1(Xi,Yi,xvi)];
%index results
X2 (i,:)=xvi(:);
Y2 (i,:)=Y(:);
end
end

Best Answer

Well, the syntax to address the row after the current is, as you've written, X(indx+1,:) but in your code
n = length(X(:,1));
for i = 1:n;
ns = length(Y(i,:));
%loop for each Y per cross-section
for j = 1:ns
%specify given data for interpolation between cross-sections
Yi= [Y(i,n),Y((i+1),n)];
Xi= [X(i,n),X((i+1),n)];
...
when i==n then i+1-->n+1 which is outside the array X bounds of 1:n as returned by
n=size(X,1);
To fix this specifically for X, write
for i=1:n-1
as the loop index so the last iteration utilizes the final two rows but is still within the bounds of the X array.
Not clear about Y; it probably has similar issues but the code snippet above uses the same indexing for it as for X but the row bound for Y is in variable ns and the loop is written over j but that indexing variable is never used. Looks like something's not right in the logic here, probably.
I couldn't decipher the actual intent from the description so not sure how to fix up the problem precisely. I'd suggest showing a small sample dataset of input/output and expected result to illustrate and so folks can have a sample case to download rather than having to try to make up data. While in general you can't prove by example, it's often (almost always?) easier to illustrate with data than by only verbiage if there isn't a specific functional form that can be written.