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
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 で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!