9 views (last 30 days)

Show older comments

Ilya on 30 Apr 2014

Edited: Ilya on 2 May 2014

Accepted Answer: W. Owen Brimijoin

Open in MATLAB Online

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 Comments Show 4 older commentsHide 4 older comments

Show 4 older commentsHide 4 older comments

Cedric on 30 Apr 2014

#### Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/127757-pad-vector-between-its-values#comment_211239

Edited: Cedric on 30 Apr 2014

Open in MATLAB Online

The example is not optimal; you'll get the answer a_desired = min(a):max(a). Let's take another one.. if you have

a = [1, 9, 5, 7] ;

is it

1 2 3 4 5 6 7 8 9 8 7 6 5 6 7

that you want?

Ilya on 30 Apr 2014

#### Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/127757-pad-vector-between-its-values#comment_211245

Edited: Ilya on 30 Apr 2014

Sorry, my bad. The question was indeed not very clear, and I've reformulated it. But basically that's it.

Cedric on 30 Apr 2014

#### Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/127757-pad-vector-between-its-values#comment_211249

In your first example, the number of points varies with the length of the interval, and not in the example with the FOR loop. How do you define this number of points? It is unclear even in the first example, as the difference between the elements is roughly 4, 4, 1, and you are using numbers of points 5, 4, 3..

Ilya on 30 Apr 2014

#### Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/127757-pad-vector-between-its-values#comment_211251

Edited: Ilya on 30 Apr 2014

EMM... The number of pts in the for loop can be set e.g. according to the length of the jth considered interval. But you can also consider that the number of padding points is constant for all intervals. This is not essential (although adjusting the number of padding pts e.g. based on some criterion would be nice). Exactly because of this I'm using arbitrary integers 5, 4, 3.

Cedric on 30 Apr 2014

#### Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/127757-pad-vector-between-its-values#comment_211256

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 on 1 May 2014

#### Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/127757-pad-vector-between-its-values#comment_211424

Edited: Ilya on 1 May 2014

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.

Sign in to comment.

Sign in to answer this question.

### Accepted Answer

W. Owen Brimijoin on 2 May 2014

Open in MATLAB Online

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 Comment Show -1 older commentsHide -1 older comments

Show -1 older commentsHide -1 older comments

Ilya on 2 May 2014

#### Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/127757-pad-vector-between-its-values#comment_211531

Edited: Ilya on 2 May 2014

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!)

Sign in to comment.

### More Answers (2)

Kelly Kearney on 1 May 2014

Open in MATLAB Online

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.

##### 0 Comments Show -2 older commentsHide -2 older comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Joseph Cheng on 1 May 2014

Open in MATLAB Online

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 Comment Show -1 older commentsHide -1 older comments

Show -1 older commentsHide -1 older comments

Joseph Cheng on 1 May 2014

#### Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/127757-pad-vector-between-its-values#comment_211447

Edited: Joseph Cheng on 1 May 2014

Open in MATLAB Online

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.

Sign in to comment.

Sign in to answer this question.

### See Also

### Categories

MATLABLanguage FundamentalsMatrices and Arrays

Find more on **Matrices and Arrays** in Help Center and File Exchange

### Tags

- vectorization
- linspace
- vector padding

### Products

- MATLAB

### Community Treasure Hunt

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

Start Hunting!

**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français

- United Kingdom(English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

Contact your local office