Find four midpoints between each two nodes

2 views (last 30 days)
if i have d = [1 1; 2 1; 2 2; 1 2]
how i can find four midpoints between each two nodes
i mean
midpoints = [1.25 1; 1.5 1;1.75 1; 2 1; 2 1.25; 2 1.5; 2 1.75; 2 2; 1.75 2; 1.5 2; 1.25 2; 1 2]

Accepted Answer

newbie9
newbie9 on 3 Aug 2019
This should work:
d = [1 1; 2 1; 2 2; 1 2];
givenMidpoints = [1.25 1; 1.5 1;1.75 1; 2 1; 2 1.25; 2 1.5; 2 1.75; 2 2; 1.75 2; 1.5 2; 1.25 2; 1 2]; % included as a check
% I like to use tables to keep track of what's in each column, but you don't have to
d = array2table(d);
d.Properties.VariableNames = {'x', 'y'};
%
midpoints = NaN(4*height(d), 2); % pre-allocate slightly oversized matrix
midpoints = array2table(midpoints);
midpoints.Properties.VariableNames = {'xm', 'ym'};
counter = 1;
for ii = 2:height(d)
% get x
x_new = linspace(d.x(ii-1), d.x(ii), 5);
x_new(end) = [];
x_new = x_new';
% get y
y_new = linspace( d.y(ii-1), d.y(ii), 5);
y_new(end) = [];
y_new = y_new';
% put into pre-allocated matrix
start_row = counter;
end_row = counter + length(x_new) -1;
midpoints.xm(start_row:end_row) = x_new;
midpoints.ym(start_row:end_row) = y_new;
counter = end_row + 1;
end
midpoints = rmmissing(midpoints); % get rid of the extra rows
Here's what it looks like:
untitled.jpg

More Answers (0)

Community Treasure Hunt

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

Start Hunting!