Expanding and interpolating between sequential values in a nx2 matrix
3 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I wish to linearly interpolate between sequential values in a given sorted 2xn matrix and a integer or non-integer sub-interval spacing value. For instance:
Given
[1 3; 2 6; 3 1; 4 9]
and an interpolation interval of, say, 0.5 (or any other value less than the minimum spacing between sequential values in column 1)
I hope to return an expanded nx2 matrix containing:
[1 3; 1.5 4.5; 2 6; 2.5 3.5; 3 1; 3.5 5; 4 9]
Note: The first column does not have to possess equally-spaced values.
I have written a function to do this using looping and it works quite well. However, when the input matrix exceeds 10,000 rows and the sub-interval is less than 0.5 the efficiency of my function drops off very quickly.
Does anyone know of a way to accomplish this goal using vectorization operations so that I can avoid using - or minimize - looping in my function?
Thank you in advance.
David
0 件のコメント
採用された回答
the cyclist
2011 年 8 月 24 日
One possible method:
x0 = [1 3; 2 6; 3 1; 4 9]; % Original array
interval = 0.5;
xi = [min(x0(:,1)):interval:max(x0(:,1))]'; % Define new spacing by interval
yi = interp1(x0(:,1),x0(:,2),xi); % Interpolated values at new spacing
newArray = [xi yi]; % New array
0 件のコメント
その他の回答 (2 件)
Paulo Silva
2011 年 8 月 24 日
m=[1 3; 2 6; 3 1; 4 9];
mm=interp2(m);
mm(:,2)=[]; %remove the midle column
mm %your result
5 件のコメント
Fangjun Jiang
2011 年 8 月 24 日
Got it. Thank you, Paulo! Honestly I can't vote for your solution though!
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!