How know the size of matrix after delete raws
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
What function do you use to know the size of the array after one or more rows are removed after a condition is achieved within a loop
Respuestas (4)
Shweta Singh
el 22 de Jun. de 2018
You can use the 'size' function as follows:
matrix_example = rand(7,8); %matrix of size 7x8
[matrix_x, matrix_y] = size(matrix_example);
% Delete a row
matrix_example(3,:) = [];
% Get the updated dimensions
[matrix_x, matrix_y] = size(matrix_example);
Hope this helps!
0 comentarios
asma khaled
el 22 de Jun. de 2018
1 comentario
Shweta Singh
el 22 de Jun. de 2018
That should be a warning and not an error. Can you share your code?
asma khaled
el 22 de Jun. de 2018
4 comentarios
dpb
el 22 de Jun. de 2018
Editada: dpb
el 23 de Jun. de 2018
Because when you got to the loop index with i = 4, you've already removed more than that number of rows so when you write
s(i,1)
where i==4 and there are only three rows left, the array addressing is outside the array dimensions and that causes the error.
To do something like this in a loop, you would want to start at the end of the array and work backwards so that the loop index is decreasing at at least the same rate as the size of the array--
nr=size(s,1); % initial number rows in s
for i=nr:-1:1 % start from end, work to beginning...
if s(i,1)>5, s(i,:)=[]; end % eliminate if desired.
end
By going backwards, you always have at least as many rows left in the array as the index value because you've removed only those of higher initial position previously for the next iteration.
BTW, don't feel badly; this is a general precept that every beginning programmer has had to learn the hard way first... :)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!