Find four midpoints between each two nodes

조회 수: 1 (최근 30일)
mohammed alany
mohammed alany 2019년 8월 3일
답변: newbie9 2019년 8월 3일
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에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by