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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by