I get the error
??? Index of element to remove exceeds matrix dimensions.
Error in ==> myfile at 111
C(i)=s{i,3};
the code being:
C=zeros(num_of_tris,1);
for i=1:size(C,1)
C(i)=s{i,3};
end
I’m not showing the code for creating s, but I assume it’s beside the point as s only appears on the right hand side of the assignment…
why does it say element to remove? which element am I removing?
Ok, so here’s what is happening.
sis probably initialized to an empty cell (NOTE: need not be entirely empty — see last paragraph). So, indexing an element ofsass{i,3}returns[]. The MATLAB operation to remove an element of a vector isSo when you loop through, you’re removing the elements of
Cone by one, and eventually, the indexiexceeds the size of the (now diminished) vector.Here’s a small example that reproduces your problem:
You’ll find that the index
iwhen you get this error isnumel(C)/2+1. In other words, tilli=5(in this example), you’re removing every odd element ofCand ati=6, the number of elements remaining inCis 5, and so you get an index out of bounds error.NOTE:
sneed not even be entirely empty. Consider this example: