フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to replace elements in a matrix using logical

1 回表示 (過去 30 日間)
Ara Jo
Ara Jo 2020 年 10 月 19 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello,
I would like to set some columns to zero for certain rows indicated by a logical. For instance, if we have a matrix X
X =
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
Suppose I have a logical that is true for row 3 and 4. Then I would like to replace column 3 to 4 with zero for row 3 and 4, so that the result is:
X =
1 1 1 1
2 2 2 2
3 3 0 0
4 4 0 0
To indicate the columns I want to replace I have tried things like 3:end, or size(B,1):end, but I get a message Subscript indices must either be real positive integers or logicals.
However, even if I only use integers, let's say 3:4, I still get the same message. What am I doing wrong? Thanks for your advice!

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 10 月 19 日
編集済み: Ameer Hamza 2020 年 10 月 19 日
Try this
X = [
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4];
tf = [false; false; true; true];
X(tf, tf) = 0;
Result
>> X
X =
1 1 1 1
2 2 2 2
3 3 0 0
4 4 0 0
  2 件のコメント
Ara Jo
Ara Jo 2020 年 10 月 19 日
Thanks for the answer! I have tried with two logicals, one for rows and another for columns, and I still get the same message..
Ameer Hamza
Ameer Hamza 2020 年 10 月 19 日
Can you paste your code which gives error?

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by