フィルターのクリア

how to delete certain columns and rows from matrix

1 回表示 (過去 30 日間)
Antrea Plastira
Antrea Plastira 2022 年 10 月 5 日
コメント済み: William Rose 2022 年 10 月 5 日
I have the matrix
M = [1000 -1000 0 0;
-1000 10000 0 -9000;
0 0 5000 -5000;
0 -9000 -5000 14000];
and I want to get the matrix when the 1st column and row and the 3rd column and row are gone, so that Mnew = [10000 -9000;
-9000 14000];
Any help?

採用された回答

William Rose
William Rose 2022 年 10 月 5 日
M = [1000 -1000 0 0;
-1000 10000 0 -9000;
0 0 5000 -5000;
0 -9000 -5000 14000];
Mnew=M([2,4],[2,4])
Mnew = 2×2
10000 -9000 -9000 14000
The line above keeps specifies the rows and columns to keep.
  2 件のコメント
Antrea Plastira
Antrea Plastira 2022 年 10 月 5 日
thank you!!
William Rose
William Rose 2022 年 10 月 5 日
@Antrea Plastira, you're welcome.

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

その他の回答 (2 件)

Steven Lord
Steven Lord 2022 年 10 月 5 日
Do you know which rows/columns you want to delete or which ones you want to keep? If to keep:
M = magic(4)
M = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
toKeep = [2 4];
A = M(toKeep, toKeep)
A = 2×2
11 8 14 1
If to delete:
M = magic(4)
M = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
toDelete = [1 3];
M(toDelete, :) = []
M = 2×4
5 11 10 8 4 14 15 1
M(:, toDelete) = []
M = 2×2
11 8 14 1

William Rose
William Rose 2022 年 10 月 5 日
If you want to sepcify the rows and columns to delete:
M = [1000 -1000 0 0;
-1000 10000 0 -9000;
0 0 5000 -5000;
0 -9000 -5000 14000];
Mnew=M;
rows2delete=[1,3];
cols2delete=rows2delete;
Mnew(rows2delete,:)=[];
Mnew(:,cols2delete)=[]
Mnew = 2×2
10000 -9000 -9000 14000
Try it.

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by