Create non-linear vector in Matlab

24 views (last 30 days)
I would like to (automatically) create a vector that has three sections, where the middle section has a higher "density" than the two surrounding sections.
Example: A=[0, 1, 2, 3, 4, 4.2, 4.4, 4.6, 4.8, 5, 6, 7, 8, 9, 10]
The spacing within each of the three sections is lin-spaced.
I am looking for a function where I can state the width of the three sections and the start and end value of each section.
Additionally, I need a 2nd vector that gives me the mid-point of every subinterval, and a 3rd vector that gives the distance from one mid-point to next.
(I do need this vector for analysing a "hot-spot" in a numercial analysis.)
Can anybody help me?

Accepted Answer

Star Strider
Star Strider on 25 Dec 2014
This may do what you want:
% DEFINE: Variable Density Vector —
% s: 3-element vector of start values
% e: 3-element vector of end values
% d: 3-element vector of density values (number of entries between start and end)
vdv = @(s,e,d) sort([linspace(s(1),e(1),d(1)) linspace(s(2),e(2),d(2)) linspace(s(3),e(3),d(3))]);
s = [1 4 6];
e = [3.9 5.9 10];
d = [3 5 3];
V = vdv(s,e,d);
It does no error checking (for instance to correct for overlapping start and end values), and it simply sorts the vector (in ascending order) to eliminate obvious problems, but it’s the only sort of ‘function’ I can imagine that will do what you want. The total length is the sum of the ‘d’ vector. Its construction and operation is straightforward, so you can alter it as you wish to make it more functional in your application.

More Answers (2)

Image Analyst
Image Analyst on 25 Dec 2014
If you created the vector with linspace() then you know how many elements are in each section - it's the third input argument of linspace().
  1 Comment
Image Analyst
Image Analyst on 25 Dec 2014
Wait a minute. Exactly what do you mean by "state"? Initially I thought you wanted the function to "state the width of the three sections and the start and end value of each section", in other words, I thought that you'd pass in the array and you wanted the function to report/state/notify/tell you how many elements were in each of the 3 subintervals, like you wanted it to determine it for you. But now I think another interpretation would be that state means to pass in as input arguments, like you pass in starting and ending values, and number of elements, for each of the 3 segments, and you want the function to build the array and return it to you as an output argument. So which is it? What does "state" mean to you?

Sign in to comment.


Tobi12
Tobi12 on 26 Dec 2014
That works great. Many thanks!
  1 Comment
Star Strider
Star Strider on 26 Dec 2014
My pleasure!
The most sincere expression of appreciation here on Matlab Answers is to Accept the Answer that most closely solves your problem.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!