pad vector between its values

조회 수: 3 (최근 30일)
Ilya
Ilya 2014년 4월 30일
편집: Ilya 2014년 5월 2일
I wonder if it's possible to pad a vector with intermediate points in order to make it more "smooth" in an elegant way (i.e. with no loops)
% Example:
a=[1.1 5.2 9.25 10];
a_desired=[1.1 2.125 3.15 4.175 5.2 5.2 6.55 7.9 9.25 9.25 9.625 10];
a_desired which was generated by:
a_desired=[linspace(a(1), a(2), 5), linspace(a(2), a(3), 4), linspace(a(3), a(4), 3)]
So, what I need is some kind of generalized linspace(), which works not only with begin and end of an interval, but allows the control of the number of points between the "node points" on the interval, where the "node points" are the begin, the end and some points in between.
In the above example, the number of entries in a is known in advance, so it's possible to use linspace() together with concatenation without any loops. But what if a is a generic vector generated by some calculation? Then I end up using loops:
a_desired=[];
for j=1:1:length(a)-1
a_desired=[a_desired, linspace(a(j), a(j+1), NUMBER_OF_PTS)];
end
which affects the performance (even if I preallocate a_desired, it still will be not very nice and not very fast).
  댓글 수: 6
Cedric
Cedric 2014년 4월 30일
Last point, why are values in a duplicated in a_desired? E.g. what don't you have just one 5.2 value but two?
Ilya
Ilya 2014년 5월 1일
편집: Ilya 2014년 5월 1일
Sorry once again, this is because of the structure of the for-loop that I'm currently using. If it's possible to implement the same functionality without duplicates (and loops...), then it's even better.

댓글을 달려면 로그인하십시오.

채택된 답변

W. Owen Brimijoin
W. Owen Brimijoin 2014년 5월 2일
If you find regular array operations more straightforward to follow than cell functions, you could create an array that increases in each column by the amount you need it to, and then unfold it back to a vector like this:
a=[1.1 5.2 9.25 10];
n_pts = 4; %specify the number of intervening points
a_desired = cumsum([a;repmat([diff(a)/n_pts,0],n_pts-1,1)]);
a_desired = a_desired(1:end-n_pts+1);
Something like that?
  댓글 수: 1
Ilya
Ilya 2014년 5월 2일
편집: Ilya 2014년 5월 2일
Yep! Actually, I thought in the same direction last time, but you've saved me time that I so desperately need to finish my thesis (and other people will now know the solution too!)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Kelly Kearney
Kelly Kearney 2014년 5월 1일
If you have the Mapping Toolbox, interpm will do that:
a2 = interpm(a, ones(size(a)), 1.025)
It's not exactly the same as your linspace example, since you set the minimum interval instead of the number of points.

Joseph Cheng
Joseph Cheng 2014년 5월 1일
you can try to use cellfun
here is a quick thing i created using your example up top. You will have to play around with it a bit to optimize it or see if the for loop is more efficient (memory wise).
a=[1.1 5.2 9.25 10]; %example a value.
%create pairs to be linspaced
a1 = a(1:end-1)';
a2 = a(2:end)';
%make pairs cells
a1 = mat2cell(a1,ones(size(a1)));
a2 = mat2cell(a2,ones(size(a2)));
%use cellfun to do the loops for you
a3=cellfun(@(x,y) linspace(x,y,100)',a1,a2,'UniformOutput',false);
a4 = cell2mat(a3)'
now a quick test shows that the stuff above, for an array that is longer than 500 elements, is faster than using the for loop you have in your question. (for me at least) there will be some variations when you vary the numbers of padding in linspace and number of items in 'a';
  댓글 수: 1
Joseph Cheng
Joseph Cheng 2014년 5월 1일
편집: Joseph Cheng 2014년 5월 1일
to get rid of the duplicates
a4 = cell2mat(a3)'
a4(100:100:length(a4)-1)=[]
which should erase the the first duplicate value and the length()-1 should stop it from deleting the last spot.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by