Using a for loop to change matrix dimesnsions

2 次查看(过去 30 天)
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