why doesn't my "if" statement work?

조회 수: 1 (최근 30일)
john borsellino
john borsellino 2013년 6월 4일
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

채택된 답변

Wayne King
Wayne King 2013년 6월 4일
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);
  댓글 수: 1
john borsellino
john borsellino 2013년 6월 4일
wow!! i will give this a try thanks..(seconds later) thankyou wayne king for such a simple answer.logical indexing worked great.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mark
Mark 2013년 6월 4일
The way you have it, you will only get the if statement to be satisfied if all elements of z are less than one. If you are trying to do this element by element, you could implement a loop through z to just change the ones that are <1.
  댓글 수: 1
john borsellino
john borsellino 2013년 6월 4일
hey mark, could you throw up some code, to explain the loop, thanks jb

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by