How to remove specific elements in a matrix

3 Ansichten (letzte 30 Tage)
George Kin
George Kin am 3 Mai 2016
Kommentiert: Catherine Mohs am 29 Jan. 2019
Say I have a matrix
A = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15];
which is a 3x5 matrix and I would like to remove the elements in A(1,2), A(2,3), A(3,4). If I only have the indices of what I want to remove in this matrix, how would I go about removing these elements to create A[1 3 4 5;6 7 9 10;11 12 13 15]; which is a 3x4 matrix. In the end, all the rows have the same same number of columns but if I remove elements piece-wise, the matrix becomes 2 rows with 5 columns and 1 row with 4 columns which MATLAB doesn't like. I tried using a logical matrix with the indices but I only get a 1x12 vector of the remaining values. Is there any easy way to do this without, say breaking up the matrix into vectors and removing the elements that way?

Antworten (2)

Walter Roberson
Walter Roberson am 3 Mai 2016
mask = zeros(size(A));
mask(sub2ind(size(A), [1 2 3], [2 3 4])) = 1;
A_t = A.';
mask_t = mask.';
new_A = reshape(A_t(~mask_t), [4 3]).' ;
You need to work in transpose space to get the columns to "fall" towards the beginning of the column.

Azzi Abdelmalek
Azzi Abdelmalek am 3 Mai 2016
Bearbeitet: Azzi Abdelmalek am 3 Mai 2016
ii=[1 2 3]
jj=[2 3 4]
[n,m]=size(A);
B=zeros(n,m-1);
for k=1:n
a=A(k,:);
a(jj(k))=[];
B(k,:)=a
end
  1 Kommentar
Catherine Mohs
Catherine Mohs am 29 Jan. 2019
Can you explain to me what exactly you are doing here? What do ii and jj do?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by