Deleting rows in a matrix
이전 댓글 표시
Hi,
I have two matrices which are generated in a loop for n years:
A (m-rows x n-columns) B (k-rows)
Matrix B includes values which represent row numbers of matrix A that should be deleted. So, I want for every value in matrix B, the corresponding row in matrix A to be deleted.
For example,
A=[1 2; 2 2; 3 4], B= [2]
The result should be a matrix [1 2;3 4]. I.e. the middle row is deleted as B includes value 2.
For some reason I'm not able to find the correct coding for this. I tried several things, but inside the loop it doesn't work. This is what I tried, but this does't work:
for j= 1:n
B;
for i= 1:length(B)
indices = find(A(:,1)==B(i));
A(indices,:) = [];
end
end
Can anyone help me with this?
댓글 수: 2
James Tursa
2015년 8월 11일
Does B really contain the row numbers, or does it contain values that must match values in the first column of A? Your words say the former but the code looks like the latter.
Bas
2015년 8월 11일
채택된 답변
추가 답변 (2개)
James Tursa
2015년 8월 11일
편집: James Tursa
2015년 8월 11일
"Matrix B includes values which represent row numbers of matrix A that should be deleted"
Assuming this is exactly what you want, then this will do the job:
A(B,:) = [];
Your code produces the result you expect. You'll need to better specify your problem. You can lose the loop completely, though, with a simple indexing:
A(B,:) = [];
This is assuming that B is unique and contains no elements larger than the length of A.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
