Azzera filtri
Azzera filtri

Find four midpoints between each two nodes

1 visualizzazione (ultimi 30 giorni)
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]

Risposta accettata

newbie9
newbie9 il 3 Ago 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

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by