Expanding and interpolating between sequential values in a nx2 matrix
이전 댓글 표시
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
채택된 답변
추가 답변 (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일
That is interesting, Paulo! Where did you get that interp2(m) with one input argument?
Paulo Silva
2011년 8월 24일
The Gods of Mathworks leave these presents for us to find :)
Found it by chance but has you can see there's a problem, one extra column must be removed from the result and that slows the code, also it doesn't work properly for other interval values than 0.5
Fangjun Jiang
2011년 8월 24일
Okay, I just want to understand how it treats the single input argument which is a 2-D array. If there is no available document, I guess I can go play and figure.
Paulo Silva
2011년 8월 24일
edit interp2 and see the magic inside :)
Fangjun Jiang
2011년 8월 24일
Got it. Thank you, Paulo! Honestly I can't vote for your solution though!
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!