Using a for loop to change matrix dimesnsions

조회 수: 2 (최근 30일)
Matthew
Matthew 2011년 3월 23일
I'm trying to "throw out" all the rows in a matrix that have all zeros. I'm getting the following error: Attempted to access C(14,:);index out of bounds because size(C)=[12,339]. I think it has to do with the fact that as I get rid of rows, the size changes. Code:
[row_c,column_c]=size(C)
for j=1:row_c m=sum(C(j,:)); if m==0 C(j,:)=[]; end [row_c,column_c]=size(C)
end

답변 (3개)

Walter Roberson
Walter Roberson 2011년 3월 23일
Do your loop in reverse so that your index will always refer to a row that has not been "moved down"

Davide Ferraro
Davide Ferraro 2011년 3월 23일
I can suggest you an approach without FOR loops that's more synthetic and is not suffering of the changing size issue:
%%Test Case
A = rand(10,10);
A([6 9],:) = 0;
A(all(A == 0,2),:) = [];
The first section is just to create a test case. With ALL I'm checking which rows are containing only 0 and then I can use indexing to remove the rows.

James Tursa
James Tursa 2011년 3월 23일
Do not do it this way. Deleting rows (or columns) in a matrix is the same bad thing as growing a matrix in a loop. MATLAB will be forced to copy the entire data set to a new memory block each time you do it, seriously dragging performance. A better way is to create a logical vector the same size as the number of rows. Then in your loop flag each row to be deleted. Then after the loop is finished delete all the flagged rows at once, so you only do the data copy once. e.g.,
[row_c,column_c]=size(C)
g = false(row_c,1);
for j=1:row_c
m=sum(C(j,:));
if m==0
g(j) = true;
end
end
C(g,:) = [];
However, having given this general advice, your specific problem can be done without a loop:
C(sum(C,2)==0,:) = [];

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by