Compare two columns in a matrix, perform if statement.

1 次查看(过去 30 天)
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
David Sanchez 2014-6-9
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by