I must to use angle = atan2(norm(cross(a,b)),dot(a,b))
, for calculating the angle between two vectors a,b
and these are double type and norm
is undefined for this type. How do I resolve this problem? I need to calculate the angle between two vectors this way.
In your comments, you have shown us how you are actually writing out the angle calculation and it is not the same as how you have put it in your post.
atan2(norm(cross(I(i,j,:),I_avg)),dot(I(i,j,:),I_avg));
I
is an image you are loading in. I'm assuming it's colour because of the way you are subsetting I
. Because I
is a 3D matrix, doing I(i,j,:)
will give you a 1 x 1 x 3
vector when in fact this has to be a 1D vector. norm
does not recognize this structure which is why you're getting this error. Therefore, you need to use squeeze
to remove the singleton dimensions so that this will become a 3 x 1
vector, rather than a 1 x 1 x 3
vector. As such, you need to rewrite your code so that you're doing this instead. Bear in mind that in your comments, angle
is always overwritten inside the for
loop, so you probably want to save the results of each pixel. With this, you probably want to create a 2D array of angles that will store these results. In other words:
I=imread('thesis.jpg');
I = double(I);
angles = zeros(m,n);
I_avg = squeeze(I_avg); %// Just in case
for i=1:m
for j=1:n
pixels = squeeze(I(i,j,:)); %// Add this statement and squeeze
angles(i,j) = atan2(norm(pixels,I_avg)),dot(pixels,I_avg)); %// Change
end
end
MATLAB has a built-in function called angle
that determines the angle from the origin to a complex number in the complex plane. It is not recommended you call your variable angle
as this will unintentionally shadow over the angle
function, and any other code that you create from this point onwards may rely on that actual angle
function, and you will get unintended results.
Using i
and j
as loop variables is not recommended. These letters are reserved for the complex number, and this can produce unintentional results. Take a look at this question and post by Shai here - Using i and j as variables in Matlab. As such, it is suggested you use other variable names instead.