필터 지우기
필터 지우기

2D matrix indexing for element wise operation

조회 수: 1 (최근 30일)
Raj Raj
Raj Raj 2016년 2월 19일
편집: Star Strider 2016년 2월 19일
Hello all, I am using following code to estimate pairwise angle between for a vector. Where the angle should be between 0 to 2*pi. I am able to predict the angle. But I need your advise on implementing this two for loops. This is slowing the code. Looking forward for your attention. In the code, XYdata is a n x 2 matrix representing the Cartesian coordinates of individual point [xi, yi]. I presume this slow is because of two FOR loops.
for i2 =1:length(XYdata_new)
for j2 = 1:length(XYdata_new)
angle_ij(i2,j2)= Angle360(XYdata_new(i2,1), XYdata_new(i2,2), XYdata_new(j2,1),XYdata_new(j2,2));
end
end
Thanks in advance.
  댓글 수: 3
Raj Raj
Raj Raj 2016년 2월 19일
function angleout = Angle360(x1,y1,x2,y2)
if x1 == x2 && y1 ==y2
angleout = Inf;
end
angle = atan((y2-y1)/(x2-x1));
if angle > 0
if (y2 < y1) % y2 > y1, x2 > x1 % dy > 0, dx > 0 %first quadrant
angleout=pi+ angle;
else %
angleout= angle; % 3rd quadrant
end
elseif angle < 0
if x2 < x1 && y2 > y1
angleout= pi + angle; % 2nd quadrant
elseif y2 < y1 && x2 > x1
angleout= 3*pi/2 + (pi/2+angle); % 4th quadrant
elseif y2 < y1 && x2-x1 == 0
angleout= pi - angle; % 4th quadrant
end
elseif angle == 0
if x2 < x1
angleout = pi +angle;
else
angleout = angle;
end
end
Raj Raj
Raj Raj 2016년 2월 19일
here it is

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

채택된 답변

Star Strider
Star Strider 2016년 2월 19일
I would use the atan2 or atan2d functions. They are vectorised, so you can likely give your matrix columns to them directly and then do the angle difference calculations.
  댓글 수: 8
Raj Raj
Raj Raj 2016년 2월 19일
Thanks Star, I have marked it as answered.
Star Strider
Star Strider 2016년 2월 19일
편집: Star Strider 2016년 2월 19일
My pleasure!
Thank you.
EDIT More efficient versions of the angle conversion lines are:
Angles360 = rem(360+a, 360);
Angles2pi = rem(2*pi+a, 2*pi);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by