Create non-linear vector in Matlab

4 次查看(过去 30 天)
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?

采纳的回答

Star Strider
Star Strider 2014-12-25
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.

更多回答(2 个)

Image Analyst
Image Analyst 2014-12-25
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 个评论
Image Analyst
Image Analyst 2014-12-25
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?

请先登录,再进行评论。


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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by