How to delete multiple row and column in matrix A and Delete row and column specified in matrix B?????????????/
7 次查看(过去 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
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
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!