How to assign Nans to specific column, not entire row

9 ビュー (過去 30 日間)
Tanika Bawa
Tanika Bawa 2022 年 3 月 11 日
コメント済み: Scott MacKenzie 2022 年 3 月 11 日
Hello!
I am trying to assign NaNs to values under 0.95, but only for the rows in column 4, not all of the columns.
Before, I used:
ZB1array = table2array(ZB1);
idx = ZB1array(:,4) < 0.95;
ZB1array(idx,: ) = NaN;
This successfully assigned NaNs, but to the entire row. In order to assign them to rows only in column 4 I tried:
ZB1array(idx, ZB1array(:,4)) = NaN;
But this returns the error 'Index in position 2 is invalid. Array indices must be positive integers or logical values.'
Does anyone know what I should do? Thanks!

採用された回答

Scott MacKenzie
Scott MacKenzie 2022 年 3 月 11 日
編集済み: Scott MacKenzie 2022 年 3 月 11 日
Your first attempt was almost correct. Here's the fix:
ZB1array = rand(5,5) % test data
ZB1array = 5×5
0.7621 0.6576 0.7379 0.1125 0.0982 0.5320 0.2791 0.8808 0.4043 0.0584 0.5336 0.4350 0.4302 0.1737 0.6654 0.7437 0.6955 0.5260 0.9517 0.3347 0.8565 0.0006 0.9451 0.0728 0.1189
idx = ZB1array(:,4) < 0.95;
ZB1array(idx,4) = NaN % must specify column 4
ZB1array = 5×5
0.7621 0.6576 0.7379 NaN 0.0982 0.5320 0.2791 0.8808 NaN 0.0584 0.5336 0.4350 0.4302 NaN 0.6654 0.7437 0.6955 0.5260 0.9517 0.3347 0.8565 0.0006 0.9451 NaN 0.1189
  2 件のコメント
Tanika Bawa
Tanika Bawa 2022 年 3 月 11 日
Oh my goodness thanks so much works perfectly!
Scott MacKenzie
Scott MacKenzie 2022 年 3 月 11 日
@Tanika Bawa, you're welcome. Glad to help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by