Find four midpoints between each two nodes

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

采纳的回答

newbie9
newbie9 2019-8-3
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by