필터 지우기
필터 지우기

How to delete multiple row and column in matrix A and Delete row and column specified in matrix B?????????????/

조회 수: 17 (최근 30일)
A =[
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7];
size(A)= 18 * 7
B=[2;3;4;5;7;9;]
size(B)= 6 * 1
The deleted row and column specified in matrix B........
I want to delete in A matrix ........
B(1)=2 ie 2 row and 2 column delete in matrix A
B(2)=3 ie 3 row and 3 column delete in matrix A
B(3)=4 ie 4 row and 4 column delete in matrix A
and so on............
  댓글 수: 1
Rik
Rik 2018년 8월 12일
You can't delete single elements in arrays. You could replace them by NaN, but as you didn't explain what you want to do, I don't know if that would fit your needs.

댓글을 달려면 로그인하십시오.

답변 (1개)

dpb
dpb 2018년 8월 12일
편집: dpb 2018년 8월 12일
On the assumption it really is to remove the full row/column, not the individual element, "Dead ahead" is sometimes the simplest...
B=sort(B,'descend'); % will need to remove rows/columns from back going forward
for i=1:length(B)
A(B(i),:)=[]; % remove row
A(:,B(i))=[]; % remove column
end
NB: Your example above will fail as B(end)>size(A,2)
If the intent is to remove the row even if there isn't such a column, then incorporate error testing or just use a try...catch block to skip the addressing error.
for i=1:length(B)
try, A(B(i),:)=[]; catch,end % remove row if extant, don't error
try, A(:,B(i))=[]; catch,end % ditto remove column
end

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by