How to divide a line into several parts and obtain the coordinates?

25 次查看(过去 30 天)
Hi, I want to create a small script that can allow me to divide a line into segments and then retrieve me the coordinates of the new points. The line is vertical so I only need the 'y' values. I create the following, but I don't know how to make the scrip save me all the results of the loop in one matrix, it just save me the last one. Here is the script:
B=[9 3]; % first point
E=[9 10]; % second point
BE=E(2)-B(2); % distance between the two points in y
No_lam=input('Enter # of laminas:');
l=BE/No_lam;
Bi=(B(2))-l;
New=[];
for n=0:No_lam
Bi=[Bi+l]
end
I will appreciate some help with this. Martha

采纳的回答

Geoff Hayes
Geoff Hayes 2014-8-21
Martha - I think that you have the write idea with your initialization of New, you just have to put it to use. Rather than New create a variable called coords as
coords = zeros(No_lam+1,1);
The above adds one to the No_lam since the subsequent for loop iterates from zero to No_lam (so No_lam + 1 points). The for loop changes just by adding a single line
for n=0:No_lam
Bi=[Bi+l];
% save Bi to the vector of coords
coords(n+1) = Bi;
end
At the end of the loop, coords contains all values from 3 to 10.
---------
Since all of your elements of coords are evenly spaced, then an alternative to the above code is to use the linspace function which will generate a vector of linearly spaced elements between two points
coords = linspace(B(2),E(2),No_lam+1)';
The result will be identical to that produced with the for loop, just simplified considerably.
Try the above and see what happens!

更多回答(1 个)

Iain
Iain 2014-8-21
This roughly what you're looking for?
for i= 1:number_of_iterations
B(i) = Bi + i*l;
end

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by