フィルターのクリア

selecting indices in a matrix and setting the rest to zero

24 ビュー (過去 30 日間)
bsriv
bsriv 2023 年 3 月 14 日
コメント済み: bsriv 2023 年 3 月 15 日
Hi,
I have a 414x22 matrix A and an array of indices
idx=[99 93 149 150 152 96 97 139 94 135 137 136 138 305 299 329 330 258 302 303 319 300 315 317 316 318]'
I would like to have all values in the matrix other than those in the rows of the indices (across columns) set to zero but I'm stuck on the code. It should be something like
new_idx=ismember(1:numel(A),idx)
A(~new_idx)=0
However that obviously treats the matrix as an array so only the first column had the numbers...

採用された回答

Torsten
Torsten 2023 年 3 月 14 日
A = rand(5,3);
idx = [2 4].';
idx_left = setdiff(1:size(A,1),idx)
A(idx_left,:) = zeros(numel(idx_left),size(A,2))

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 3 月 14 日
new_A = zeros(size(A), 'like', A);
new_A(idx,:) = A(idx,:);

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by