how to delete certain columns and rows from matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
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])
The line above keeps specifies the rows and columns to keep.
댓글 수: 2
추가 답변 (2개)
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)
toKeep = [2 4];
A = M(toKeep, toKeep)
If to delete:
M = magic(4)
toDelete = [1 3];
M(toDelete, :) = []
M(:, toDelete) = []
댓글 수: 0
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)=[]
Try it.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!