Compare two columns in a matrix, perform if statement.

5 Ansichten (letzte 30 Tage)
Jeremy
Jeremy am 9 Jun. 2014
Kommentiert: Jeremy am 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.

Akzeptierte Antwort

David Sanchez
David Sanchez am 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

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by