Delete multiple rows from array

I need to delete multiple rows from an array. I know I cannot use a loop because the size of the array will change.
Does anyone have any suggestions on how to accomplish this?
UPDATED: For example, if I have an array such as:
1 9 0
2 1 2
5 8 3
7 1 0
Depending on if the 3rd column is a 0 (I used num2cell, so I'm not sure what type that makes each item in the cells), then I want to delete the whole row. So in the above example,
1 9 0
7 1 0
Would both be deleted, and the array would then only be a 2x3 array.
Thanks in advance! Let me know if I need to add more details. -Matt

2 comentarios

the cyclist
the cyclist el 27 de Feb. de 2012
Could you give more detail on what your array looks like (or, ideally, a small example)? Is "array" really a cell array, as the comment suggests? If so, then it seems that v1,v2 and v3 are also cell arrays. If that's the case, then what's inside the cells?
Sean de Wolski
Sean de Wolski el 27 de Feb. de 2012
might want to look at ismember() to build up your index2delete vector

Iniciar sesión para comentar.

 Respuesta aceptada

per isakson
per isakson el 27 de Feb. de 2012
Two possible approaches are:
array( v3==0, : ) = [];
and
for ii = size( array, 1 ) : -1 : 1
if v3{ii}==0
array( ii, : ) = [];
end
end
Loop from bottom up seems to be the simplest way, if the array is not too big.
========================= UPDATE
Yes, it is possible to use v3. However, it "smells". My point was to loop from bottom up, which I thought was appropriate to point out since you excluded looping.
The code below does what I think you are asking for
num = [
1 9 0
2 1 2
5 8 3
7 1 0 ];
cac = num2cell( num );
ca1 = cellfun( @(x) num2str(x), cac, 'uni', false );
iscellstr( ca1 )
ca2 = ca1;
for ii = size( ca1, 1 ) : -1 : 1
if strcmp( ca1(ii,3), '0' )
ca1( ii, : ) = [];
end
end
ca2( strcmp( ca2(:,3), '0' ), : ) = [];
strcmp( ca1, ca2 )
all( strcmp( ca1, ca2 ) )
all( all( strcmp( ca1, ca2 ) ) )

4 comentarios

Matt
Matt el 27 de Feb. de 2012
Is it still possible to reference v3 after it has been added into the array?
Either way, when I try this I get this error:
"Undefined function 'eq' for input arguments of type 'cell'."
I have chars in the cell, so I cannot simply use 0.
Thanks for the quick response!
Matt
Matt el 28 de Feb. de 2012
Actually, it will be easier to think of my array as only a two dimensional array, so it is structured like
1 9 0
2 1 2
5 8 3
7 1 0
And depending on if the 3rd column is a 0 (I used num2cell, so I'm not sure what type that makes each item in the cells), then I want to delete the whole row.
So in the above example,
1 9 0
7 1 0
Would both be deleted, and the array would then only be a 2x3 array.
Thanks!
-Matt
per isakson
per isakson el 28 de Feb. de 2012
I'm still not sure whether you want to compare double or characters.
Matt
Matt el 28 de Feb. de 2012
This is what I was looking for! Thanks for your help! I shouldn't have disregarded loops as you said. This worked great, thanks again!
-Matt

Iniciar sesión para comentar.

Más respuestas (1)

prasoon purwar
prasoon purwar el 27 de Feb. de 2012
if isequal(v3,0)==1
array(n,:)=[]
end % array is your matrix
% n is the row , which is to be removed

1 comentario

Matt
Matt el 27 de Feb. de 2012
Should I do this inside of a loop? As I said above, can I still access v3 by name after it has been pushed into array?
Thanks for the quick response!

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 27 de Feb. de 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by