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.
0 个评论
采纳的回答
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!