Interpolating from different rows in a for loop

4 views (last 30 days)
Hello!
I want to interpolate between multiple cross-sections. Each cross-section is a 10x100 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

Accepted Answer

dpb
dpb on 4 Sep 2017
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.
  2 Comments
Si Cla
Si Cla on 5 Sep 2017
Edited: Si Cla on 5 Sep 2017
Thankyou for your response! I've attached some sample data for X and Y to the original post, as well as an image describing the output.
The addition of n-1 has solved the error message. Additionally, replacing the "n" in the previous code with j means that the code starts the interpolation at the first row of Y, which before it didn't.
However, the code still fails to apply this to all other rows. If you see the image attached above you can see there is only interpolation for the first row. So the problem seems to be with the second for loop, as it goes from cross section to cross section, but doesn't connect the Y down each cross section. The idea for the code is:
1) loop for each cross section (e.g. i, then i+1, i+2,...)
n = length(X(:,1));
for i = 1:n2-1;
2) loop for each Y for the given cross-section (e.g. j, j+1, j+2,...)
ns = length(Y(i,:));
for j = 1:ns
3) in the same loop, create linearly spaced vectors between pairs of j for cross-section i and i+1
Xa = X(i,j);
Xb= X((i+1),j);
xvi=linspace(Xa,Xb,100);
4) in the same loop, interpolate Y along the newly created linearly spaced vectors
Yi= [Y(i,j),Y((i+1),j)];
Xi= [X(i,j),X((i+1),j)];
Y=[interp1(Xi,Yi,xvi)];
Repeat for all other cross-sections. All together:
%loop for each cross-section as defined by X (e.g. i, then i+1, i+2...)
n = length(X(:,1));
for i = 1:n-1;
ns = length(Y(i,:));
%loop to interpolate each Y pairing between given cross sections (e.g. j and j+1)
ns = length(Y(i,:))
for j = 1:ns
%specify data to interpolate between for cross-sections (i.e. j and j+1)
Yi= [Y(i,j),Y((i+1),j)];
Xi= [X(i,j),X((i+1),j)];
%specify data points to create query points between (i.e. i and i+1)
Xa = X(i,j);
Xb= X((i+1),j);
%create linearly spaced vectors between cross-section i and i+1
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
Si Cla
Si Cla on 5 Sep 2017
I had another look and the issue was stemming from my indexing. Changing it to index each respective J, and then index each respective X and it's working fine. Thanks for the advice!

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!