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

11 ビュー (過去 30 日間)
VIJAY
VIJAY 2018 年 8 月 12 日
編集済み: dpb 2018 年 8 月 12 日
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 Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by