Adapt size of vector with interp1

7 ビュー (過去 30 日間)
Riccardo Zabatta
Riccardo Zabatta 2022 年 1 月 17 日
回答済み: Image Analyst 2022 年 1 月 17 日
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?

回答 (2 件)

Cris LaPierre
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);
xlim([0 2*pi]);
title('(Default) Linear Interpolation');

Image Analyst
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]
newv1 = 20×2
0.0879 0.3658 0.0936 0.3633 0.1345 0.3454 0.1366 0.3444 0.1539 0.3369 0.1830 0.3281 0.2336 0.4648 0.2376 0.4756 0.2886 0.5179 0.3030 0.4804
% Plot interpolated points in magenta.
plot(x2, newy1, 'm.-', 'MarkerSize', 35);




Community Treasure Hunt

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

Start Hunting!

Translated by