MATLAB: Why doesn’t the “if” statement work

if statement...control flow

i have 2 columns that i am performing an element-wise foldchange analysis on columns A & B. if the result is less than 1 then the operation must be reversed. i have; 1. z=B./A % divide each element of B by corresponding A element 2. if z< 1 % if Z is less than 1 3. A./B*-1 % then divide A element by corresponding B element and multiply by -1 end
but i get no response from matlab. this should be easy but it's not. as always thanks for your time and input jb

Best Answer

You don't need an if statement, you can use logical indexing.
A = randn(8,1);
B = randn(8,1);
Z = B./A;
Z(Z<1) = A(Z<1)./B(Z<1).*(-1);