remove row with matching string
Ältere Kommentare anzeigen
I'm trying to eliminate an entire row of data if a string is found in the cell array.
Sample array values:
myarray =
[ 16] '10/05/2011' '11:20' 'waiting' 'nopos' [ 10000]
[ 17] '10/05/2011' '11:25' 'open' 'long' [ 0]
[ 34] '10/05/2011' '12:50' 'open' 'short' [ 0]
so i'd like to have:
my array =
[ 17] '10/05/2011' '11:25' 'open' 'long' [ 0]
[ 34] '10/05/2011' '12:50' 'open' 'short' [ 0]
there can be several occurances in the array, I'm not looking to pop the top row...
you help is greatly appreciated
1 Kommentar
Image Analyst
am 5 Apr. 2012
So which element in the cell array are you checking for the string? In column 4 or column 5, or something else.
Antworten (2)
Jan
am 5 Apr. 2012
array(strcmp(array(:, 4), 'waiting'), :) = [];
Richard Brown
am 5 Apr. 2012
If it can occur anywhere, use cellfun to test for equality:
[I, ~] = find(cellfun(@(s) isequal(s, 'waiting'), myarray));
myarray(I, :) = [];
otherwise, you can pull out the column of interest and use ismember
I = ismember(myarray(:, 4), 'waiting')
myarray(I, :) = [];
4 Kommentare
Jan
am 5 Apr. 2012
There is no need to omit a trailing output: I=FIND() looks easier than [I, ~]=FIND().
ISMEMBER performs a time-consuming sorting of the data.
Richard Brown
am 5 Apr. 2012
(sorry for dup comment - commented on the wrong answer by accident with my mobile!)
There is a big difference between find with one output, and find with two outputs. I is only to contain the rows, not linear indices.
Not certain I believe you re ismember here - the array to be searched has only one entry. I might check when I'm next in front of a computer. At any rate, you're right that strcmp is more conventional.
Richard Brown
am 5 Apr. 2012
And yes, just checked - for a cell array vector of 100000 strings, on my computer strcmp takes 0.01 seconds, whereas ismember takes 0.03 :)
Jan
am 5 Apr. 2012
0.02 seconds lost. But if you do this 10e6 times... :-)
I've overseen, that your FIND command operates on a matrix. Then the 2nd omitted output does matter, as you have written.
Kategorien
Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!