How can I divide a given range of three values into equal subparts like abc=[initial_values, mid_value, last_value] and expand the range into a total of 9 or 12 values?
17 views (last 30 days)
Show older comments
Hello,
I want to display the given values in a graph. However, the result contains only three values which does not look good on the graph. I want to display multiple points on the graph.
x = [80 160 240];
y = [0.0623671622299242 0.109342051940389 0.158597892409503];
plot(x,y);
I want the graph to have the original values as well as values in between the given values i.e. x = 80, 100, 120, 140, 160, 180, 200, 220, 240... I want to do it through code not manually. Can anyone help?
(PS: I'm a newbie)
Accepted Answer
Max Alger-Meyer
on 7 Mar 2022
Edited: Max Alger-Meyer
on 7 Mar 2022
The simplest way of doing it would be to create a single vector of two combined, linearlly spaced vectors, and then deleting one of the duplicate points that make up the end of the first vector and the start of the second vector.
x = [80 160 240];
length = 9; %This number should stay odd if you want the middle value of x to stay in the middle
x2 = [linspace(x(1),x(2),(length+1)/2) linspace(x(2),x(3),(length+1)/2)]
The above vector is almost there but as you can see, the middle value of 160 is repeated twice. We can go in and delete this manually by applying the follwing:
x2(numel(x2)/2) = []
1 Comment
Max Alger-Meyer
on 7 Mar 2022
I'm not entirely sure what you mean when you say " I am still unable to assign or store the value (x2(numel(x2)/2) = []) to a new variable". The code in parentheses shouldn't be set to a variable, as it is instead a command to tell Matlab to delete the first of the two identical values in the array x2. The final code should just looks as follows.
x = [80 160 240];
length = 9; %This number should stay odd if you want the middle value of x to stay in the middle
x2 = [linspace(x(1),x(2),(length+1)/2) linspace(x(2),x(3),(length+1)/2)];
x2(numel(x2)/2) = [];
x2 %This is just to demonstrate that the array is as desired. You don't need this line in your code.
To clarify what is happening, numel(x2) just returns the total number of elements in x2 (in this case it is 10)
x = [80 160 240];
length = 9; %This number should stay odd if you want the middle value of x to stay in the middle
x2 = [linspace(x(1),x(2),(length+1)/2) linspace(x(2),x(3),(length+1)/2)]
numel(x2)
numel(x2)/2
x2(numel(x2)/2)
Since our array x2 is first made up of two arrays of the same length, we will always have an even number of array elements, with the middle two being identical. That fourth line is referencing the first of the two identical array indices. We could just set x2(5) = [], however it is good practice to make this code work for any length array we want. so instead of calling out 5 specifically, we use numel(x2)/2 (which in this case is equal to 5). Here is an example with a shorter array:
x = [80 160 240];
length = 5; %This number should stay odd if you want the middle value of x to stay in the middle
x2 = [linspace(x(1),x(2),(length+1)/2) linspace(x(2),x(3),(length+1)/2)];
x2(numel(x2)/2) = []
More Answers (1)
Stephen23
on 7 Mar 2022
The MATLAB approach is to use INTERP1, because what you ask about is exactly what interpolation is for:
x = [80,160,240];
y = [0.0623671622299242,0.109342051940389,0.158597892409503];
xq = unique([x(1):20:x(end),x]);
yq = interp1(x,y,xq);
plot(xq,yq,'-*')
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!