MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

Exclude one row from calculation

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.

  0 件のコメント

サインイン to comment.

1 件の回答

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 件のコメント

Max Bornemann 2019 年 2 月 22 日
Thank you for your answer.
It was just coincidental that Reference = 2 AND at the same time the second row has the value 2 in my example.
Imagine the second row has different values than 2 and maybe also more than one column like:
ABC=[1 1 1; 3 4 4; 3 3 3; 4 4 4; 5 5 5; 6 6 6]
I don't get in this case.
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 2019 年 2 月 22 日
Didn't know all of any functions. Thanks again! :-)

サインイン to comment.



Translated by