필터 지우기
필터 지우기

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

조회 수: 15 (최근 30일)
Martha
Martha 2014년 8월 21일
댓글: Martha 2014년 8월 21일
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