pad vector between its values (2024)

9 views (last 30 days)

Show older comments

Ilya on 30 Apr 2014

  • Link

    Direct link to this question

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

  • Link

    Direct link to this question

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

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

Cedric on 30 Apr 2014

Direct link to this comment

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

  • Link

    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

  • Link

    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

Cedric on 30 Apr 2014

Direct link to this comment

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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    Direct link to this answer

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

  • Link

    Direct link to this answer

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

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

Ilya on 2 May 2014

Direct link to this comment

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

  • Link

    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

  • Link

    Direct link to this answer

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

  • Link

    Direct link to this answer

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

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

Sign in to comment.

Joseph Cheng on 1 May 2014

  • Link

    Direct link to this answer

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

  • Link

    Direct link to this answer

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

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

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

  • Link

    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.


pad vector between its values (13)

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

pad vector between its values (2024)

FAQs

How does padarray work in MATLAB? ›

The padarray function pads numeric or logical images with the value 0 and categorical images with the category <undefined> . By default, paddarray adds padding before the first element and after the last element of each dimension.

What is padding in MATLAB? ›

The output frame has the same dimensions as the input frame. The padding operation assigns a pattern of pixel values to the inactive pixels around a frame. Vision HDL Toolbox algorithms provide padding by constant value, replication, or symmetry.

How is a vector represented in MATLAB? ›

As mentioned earlier in the section of inputting vectors and matrices, MATLAB uses square brackets, [ ] to create a vector. For example, to create the vectors A = -2i + 6j, B = 6i + 3j, C = 4i – 3j, and D = -2i – 4j shown in Figure 10, type in the MATLAB command window.

How to assign values to an array in MATLAB? ›

To assign a value to every element of a multidimensional Java array, use the MATLAB colon operator ( : ). For example, initialize the contents of dblArray to zero.

How to create an array of variables in MATLAB? ›

To create an array with multiple elements in a single row, separate the elements with either a comma ',' or a space. This type of array is called a row vector. To create an array with multiple elements in a single column, separate the elements with semicolons ';'. This type of array is called a column vector.

What does padding () do? ›

Note: Padding creates extra space within an element. In contrast, margin creates extra space around an element.

What is an example of padding? ›

the padding on the seat of the chairs These shoes have extra padding in the heel. If you remove the padding from his speech you can see that he offers no new ideas.

What is array padding? ›

The array padding transformation sets a dimension in an array to a new size. The goal of this transformation is to reduce the number of memory system conflicts. The transformation is applied to a full function AST. The new size can be specified by the user or can be computed automatically.

How do you create a vector of values in MATLAB? ›

You can create a vector both by enclosing the elements in square brackets like v=[1 2 3 4 5] or using commas, like v=[1,2,3,4,5]. They mean the very same: a vector (matrix) of 1 row and 5 columns. It is up to you.

What is considered a vector in MATLAB? ›

A vector is a two-dimensional array that has a size of 1-by-N or N-by-1, where N is a nonnegative integer.

What is work vector in MATLAB? ›

All DWork vectors are S-function memory that the Simulink® engine manages. The Simulink software supports four types of DWork vectors: General DWork vectors contain information of any data type. DState vectors contain discrete state information.

How does strcmp work MATLAB? ›

tf = strcmp( s1,s2 ) compares s1 and s2 and returns 1 ( true ) if the two are identical and 0 ( false ) otherwise. Text is considered identical if the size and content of each are the same. The return result tf is of data type logical .

How does histogram work in MATLAB? ›

histogram( X ) creates a histogram plot of X . The histogram function uses an automatic binning algorithm that returns bins with a uniform width, chosen to cover the range of elements in X and reveal the underlying shape of the distribution.

How does Xlim work in MATLAB? ›

xlim( limits ) sets the x-axis limits for the current axes or chart. Specify limits as a two-element vector of the form [xmin xmax] , where xmax is greater than xmin . xlim( limitmethod ) specifies the limit method MATLAB® uses for automatic limit selection.

How do MATLAB matrices work? ›

The MATLAB environment uses the term matrix to indicate a variable containing real or complex numbers arranged in a two-dimensional grid. An array is, more generally, a vector, matrix, or higher dimensional grid of numbers.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Delena Feil

Last Updated:

Views: 5580

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.