The code below explains program to evaluate a 2x2 rotation matrix for given input angle.The code seems to be fine for all elements but one in1st row and second column.Can't figure out where I'm wrong.

조회 수: 3 (최근 30일)
function [ var ] = rot( theta )
for j=1:2,
for i=j:2,
if (i==j)
var(i,j) = cosd(theta);
elseif (i-j>0)
var(i,j) = sind(theta);
else
var(i,j) = -sind(theta);
end
end
end
var;
end
The problem, is with var(1,2) which always displays a zero irrespective of angle value.

채택된 답변

Stephen23
Stephen23 2015년 10월 3일
편집: Stephen23 2015년 10월 3일
Using loops is very inefficient code. Try this instead:
function mat = rotmat2d(theta)
assert(isnumeric(theta)&&isscalar(theta),'Value must be a scalar numeric')
mat = [cosd(theta),sind(theta);-sind(theta),cosd(theta)];
end
and tested:
>> rotmat2d(0)
ans =
1 0
0 1
>> rotmat2d(90)
ans =
0 1
-1 0
>> rotmat2d(45)
ans =
0.70711 0.70711
-0.70711 0.70711
>> rotmat2d(180)
ans =
-1 0
0 -1
  댓글 수: 2
Jai  Khullar
Jai Khullar 2015년 10월 3일
Seems I was taking the hard way. Is there really a need of assert command? please do answer this one! I'm new in matlab. Anyways it was helpful. Thanks!
Stephen23
Stephen23 2015년 10월 3일
편집: Stephen23 2015년 10월 3일
The assert command fits into the category of "your function will work without it, but it is highly highly recommended". Using assert and checking input arguments is certainly a good habit to get into. The trick is to make the checking as minimal as possible, to allow your function to be a general as possible.

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

추가 답변 (0개)

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by