Adapt size of vector with interp1
조회 수: 44 (최근 30일)
이전 댓글 표시
I would like to resize a series of vector arrays I have obtained from digitalising a series of curves from a graph.
Let's say I have two curves on the same x-y plane, so I have two arrays reporting the x,y evolution.
The first array (name v1) has 10 rows (so I used 10 points to digitalise the first curve)
The second array (name v2) has 20 rows (so I used 20 points to digitalise the second curve)
I want v1 to be of the same length of v2.
What I have tried is to define the array v1_new. Here I report an example of the code, generating the arrays with the rand function
v1 ={ rand(10,2),rand(10,2)};
v2 = { rand(20,2),rand(20,2)};
v1_new{1}(:,1) = interp1(min(v1{1}(:,1)),max(v1{1}(:,1)),numel(v2{1}(:,1)));
v1_new{1}(:,2) = interp1(min(v1{1}(:,2)),max(v1{1}(:,2)),numel(v2{1}(:,2)));
I get the following error:
Error using matlab.internal.math.interp1
Interpolation requires at least two sample points for each grid dimension.
Any ideas of how to fix this code and to make the problem solved?
댓글 수: 0
답변 (2개)
Cris LaPierre
2022년 1월 17일
I don't think using random number matrices creates a meaningful example here. Consider this example from the interp1 documentation page.
% original vectors have length of 9
x = 0:pi/4:2*pi;
v = sin(x);
% Want new vectors to have length 17
% Define new x vector
xq = 0:pi/8:2*pi;
% Use interp1 to determine corresponding y values
vq1 = interp1(x,v,xq);
plot(x,v,'o',xq,vq1,'x');
xlim([0 2*pi]);
title('(Default) Linear Interpolation');
legend("Original","Interp1")
댓글 수: 0
Image Analyst
2022년 1월 17일
Why are you using cell arrays? rand(10,2) is an array of 10 points with x in the first column and y in the second column. Then you create another set and put each set into a cell of a 1x2 cell array. So v1 has two sets of points, and v2 has two sets of points. Maybe you meant to have two double 10x2 arrays, not cell arrays each with two sets of points.
% Create sample data:
v1 = sortrows(rand(10,2), 1);
v2 = sortrows(rand(20,2), 1);
% Get x and y from the columns:
x1 = v1(:, 1);
y1 = v1(:, 2);
x2 = v2(:, 1);
y2 = v2(:, 2);
% Plot them each.
plot(x1, y1, 'r.-', 'MarkerSize', 35);
grid on;
hold on;
plot(x2, y2, 'b.-', 'MarkerSize', 35);
% Interpolate the y1 to get estimates at the locations of x2:
newy1 = interp1(x1, y1, x2);
% Place back into a 10x2 double array.
newv1 = [x2, newy1]
% Plot interpolated points in magenta.
plot(x2, newy1, 'm.-', 'MarkerSize', 35);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!