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

20 次查看(过去 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