# Compare two columns in a matrix, perform if statement.

6 views (last 30 days)
Jeremy on 9 Jun 2014
Commented: Jeremy on 9 Jun 2014
I have a matrix (A) comprised of two columns each containing values which range from above and below zero. I want to perform different operations for the columns depending on which is above or below zero, and then put the answer in a third column. But currently, the code I am writing does not work. Any help much appreciated.
Example data: -0.758061618089340 -0.268100439060114 0 -0.721712386617467 -0.255203506765691 0 -2.45015138605334 5.44561062848519 0
Code tried: if (A(:,1)>0) && (A(:,2))<0) A(:,3) = (atan(A(:,1)/A(:,2))/(pi*180))+180;
Produces the following error: Operands to the and && operators must be convertible to logical scalar values.
Thanks for any help.

David Sanchez on 9 Jun 2014
Your sample data is not very useful, but I think you are trying to do something like this:
%sample data
A=[-0.758061618089340 -0.268100439060114;
0 -0.721712386617467;
-0.255203506765691 0;
5.44561062848519 -1];
N_col = size(A,1);
A=[A, zeros(N_col,1)];
for k=1:N_col
if (A(k,1)>0) && (A(k,2)<0)
A(k,3) = (atan(A(k,1)/A(k,2))/(pi*180))+180;
end
end
Jeremy on 9 Jun 2014
Thank you. Worked perfectly.