Make specific columns of a matrix zero.

10 ビュー (過去 30 日間)
Santos García Rosado
Santos García Rosado 2021 年 3 月 4 日
Hi Mathworks community!
So I have a matrix A such as:
A = [1,2,3,4,5,6,7,8,9; 5,8,6,44,8,6,8,7,3; 9,8,7,6,5,4,3,2,1]
And an array b that specifies the columns I' DON´T want to make zero:
b = [1,4,5,9]
Getting an output such as:
out = [1,0,0,4,5,0,0,0,9; 5,0,0,44,8,0,0,0,3; 9,0,0,6,5,0,0,0,1]
I'm working with much bigger dimensions so, Any ideas of how to do this efficiently?
Thank you!
Santos

採用された回答

Stephen23
Stephen23 2021 年 3 月 4 日
A = [1,2,3,4,5,6,7,8,9; 5,8,6,44,8,6,8,7,3; 9,8,7,6,5,4,3,2,1];
b = [1,4,5,9];
out = zeros(size(A));
out(:,b) = A(:,b)
out = 3×9
1 0 0 4 5 0 0 0 9 5 0 0 44 8 0 0 0 3 9 0 0 6 5 0 0 0 1
  1 件のコメント
Santos García Rosado
Santos García Rosado 2021 年 3 月 4 日
Thank you Stephen! Exactly what I was looking for.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by