Exclude one row from calculation

조회 수: 3 (최근 30일)
Max Bornemann
Max Bornemann 2019년 2월 22일
댓글: Max Bornemann 2019년 2월 22일
Hello, i am new in MATLAB. I am searching for a way to calculate with every but one row (in this example it is the second row).
This is my code, which doesn`t work:
Reference=2;
ABC=[1;2;3;4;5;6];
logodds(1:5,:)=log(ABC(:~=Reference,1)./ABC(Reference,1));
What i could do would be:
logodds(1:5,:)=log(ABC([1 3 4 5 6],1)./ABC(Reference,1));
But the first part of the calculation ABC([1 3 4 5 6],1) musst be dependent on "Reference" because the value of Reference can change between 1 and 6. It should be a solution, where i don´t have to enumerate every row seperately. I want to express something like: Use for calculation every row (:) except the Reference-row (~=Reference).
I will greatly appreciate any assistance.

채택된 답변

Rik
Rik 2019년 2월 22일
The easiest way to do this is with a logical vector:
Reference=2;
ABC=[1;2;3;4;5;6];
L=ABC~=Reference;
logodds(1:5,:)=log(ABC(L,1)./ABC(Reference,1));
However, this might fail for decimal values that have float rounding errors. In that case you may want to use ismembertol to generate the vector, or use
L= abs(ABC-Reference) < 2*eps;
  댓글 수: 3
Rik
Rik 2019년 2월 22일
You can use the all or any functions to convert L back to a vector to suit your needs. This method does not rely on the 2 being in the second row, so that does not matter.
Max Bornemann
Max Bornemann 2019년 2월 22일
Didn't know all of any functions. Thanks again! :-)

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by