Create non-linear vector in Matlab

조회 수: 10 (최근 30일)
Tobi12
Tobi12 2014년 12월 25일
댓글: Star Strider 2014년 12월 26일
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