필터 지우기
필터 지우기

Saving values in a vector inside a loop, and finding the specific loop at which a certain element is saved

조회 수: 1 (최근 30일)
I have a code that calculates the minimum distance between two line segments by discretizing t, and s between 0 and 1 with h. The code saves the distance for each value of s and t in a vector and the smallest value is picked out at the end.
I would like to find the corresponding t and s for which the minimum distance occurs. For example, if the minimum distance is located at index 3000 in the 'mindist' vector, which value of t and s does this correspond?
Thanks in advance! /Arian
h=0.01;
mindist=[];
for t=0:h:1
for s=0:h:1
if F==0
s=0;
t=e/c;
mindist=[mindist; norm((P0+s*u)-(Q0+t*v))];
else
mindist=[mindist; norm((P0+s*u)-(Q0+t*v))];
end
end
end
[mindist,loc]=min(mindist);
mindist

채택된 답변

Birdman
Birdman 2018년 2월 14일
Try this(don't forget to define e, c , u , v etc):
h=0.01;
t=0:h:1;
s=zeros(1,numel(t));
mindist=[];
for i=1:numel(t)
for j=1:numel(s)
if F==0
t(i)=e/c;
mindist(i,j)=[mindist; norm((P0+s(j)*u)-(Q0+t(i)*v))];
else
mindist(i,j)=[mindist; norm((P0+s(j)*u)-(Q0+t(i)*v))];
end
end
end
[mindist,loc]=min(mindist);
mindist
  댓글 수: 1
Arian Abedin
Arian Abedin 2018년 2월 14일
편집: Arian Abedin 2018년 2월 14일
Hey and thanks for the response, works perfectly! I used sub2ind to get the row and colum (i and j). Also had to change s=zeros(1,numel(t)) to s=0:h:1. Thanks alot again. This is what Ive got:
h=0.01;
t=0:h:1;
s=0:h:1;
mindist=[];
for i=1:numel(t)
for j=1:numel(s)
if F==0
t(i)=e/c;
mindist(i,j)=norm((P0+s(j)*u)-(Q0+t(i)*v));
else
mindist(i,j)=norm((P0+s(j)*u)-(Q0+t(i)*v));
end
end
end
[minval,loc]=min(mindist(:))
[i, j] = ind2sub(size(mindist), loc)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jos (10584)
Jos (10584) 2018년 2월 14일
A few thoughts:
  • There is a potential problem in your code as you set the loopcounters s and t to a value within the loop.
  • F does not change within the loop, so the if can be taken out of the loop
The easiest way given your current code: store the values of i and s alongside the output of norm, so mindist will be a N-by-3 vector
...
mindist = [mindist ; norm(...) s t]
...
[~, loc]=min(mindist(:,1)) ;
mindist = mindist(loc) % three values
However, as you do not provide any information about the variables P0, Q0, e, c, u and v, the whole algorithm might be written much more efficiently! For instance by pre-allocating the output and use an index counter into the possible values of s:
srange = 0:h:1 ;
mindist = zeros(numel(srange),1) ;
for s_k = 1:numel(srange)
s_value_to_use = srange(s_k) ;
mindist(s_k,:) = ...
end
  댓글 수: 1
Arian Abedin
Arian Abedin 2018년 2월 14일
Hey and thanks for your response. Yea the code is probably pretty inefficient and poorly written. I'll take your suggestions into consideration. Got this now from Birdman's answer and it at least seems to do the trick, will look into making it better overall, thanks again.
h=0.01;
t=0:h:1;
s=0:h:1;
mindist=[];
for i=1:numel(t)
for j=1:numel(s)
if F==0
t(i)=e/c;
mindist(i,j)=norm((P0+s(j)*u)-(Q0+t(i)*v));
else
mindist(i,j)=norm((P0+s(j)*u)-(Q0+t(i)*v));
end
end
end
[minval,loc]=min(mindist(:));
[i, j] = ind2sub(size(mindist), loc);

댓글을 달려면 로그인하십시오.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by