Compare two columns in a matrix, perform if statement.

5 ビュー (過去 30 日間)
Jeremy
Jeremy 2014 年 6 月 9 日
コメント済み: Jeremy 2014 年 6 月 9 日
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
  1 件のコメント
Jeremy
Jeremy 2014 年 6 月 9 日
Thank you. Worked perfectly.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by