Interpolating from different rows in a for loop
Afficher commentaires plus anciens
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

Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!