Info

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

how to delete an entire column from a matrix if there is a certain number in that column?

1 回表示 (過去 30 日間)
Joseph
Joseph 2017 年 6 月 2 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
lets say i have a matrix as below:
A =
1 1 1 1 1
2 2 2 2 2
3 3 3 9 3
i wan to delete the entire column if there is element with the value of 9 in it and get the matrix B as below:
B =
1 1 1 1
2 2 2 2
3 3 3 3
i should mention that in reality may matrix size is much greater than this with the size of A(150,32024), so i need to do it in a for loop, or if anyone has any other idea, i really appreciate it.
thank you
  1 件のコメント
Guillaume
Guillaume 2017 年 6 月 2 日
編集済み: Guillaume 2017 年 6 月 2 日
so i need to do it in a for loop. The size of the matrix is irrelevant to the method you use for solving your problem. In fact, the bigger the matrix, the more likely that using for loops is the wrong solution.
Star's and dpb answers are going to be a lot more efficient than a loop.

回答 (2 件)

Star Strider
Star Strider 2017 年 6 月 2 日
See if this does what you want:
B = A(:,~any(A==9,1))

dpb
dpb 2017 年 6 月 2 日
B=A(:,~any(A==9));
If you don't need A any longer, it's just a simple to do it in place...
A(:,any(A==9))=[];

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

Community Treasure Hunt

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

Start Hunting!

Translated by