How to remove specific elements in a matrix
조회 수: 3 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (2개)
Walter Roberson
2016년 5월 3일
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.
댓글 수: 0
Azzi Abdelmalek
2016년 5월 3일
편집: Azzi Abdelmalek
2016년 5월 3일
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
Catherine Mohs
2019년 1월 29일
Can you explain to me what exactly you are doing here? What do ii and jj do?
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!