Using a for loop to change matrix dimesnsions
2 views (last 30 days)
Show older comments
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
0 Comments
Answers (3)
Walter Roberson
on 23 Mar 2011
Do your loop in reverse so that your index will always refer to a row that has not been "moved down"
0 Comments
Davide Ferraro
on 23 Mar 2011
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.
0 Comments
James Tursa
on 23 Mar 2011
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,:) = [];
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!