Using a for loop to change matrix dimesnsions
1 次查看(过去 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
0 个评论
回答(3 个)
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"
0 个评论
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.
0 个评论
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,:) = [];
0 个评论
另请参阅
类别
在 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!