How to combine cellfun and strfind?

  • Follow


Hi there.
I have 1x7 cell array with the contents:

'.'	'..'	'H66000.526'	'H66001.526'	'H66002.526'	'H66003.526'	'H66004.526'

How can I combine cellfun and strfind to find '001.' in the other strings, please?

Thank you.
0
Reply bluesaturn 6/2/2010 10:12:29 AM

bluesaturn[at]kellnerweg.de wrote:

> I have 1x7 cell array with the contents:
> 
> '.'	'..'	'H66000.526'	'H66001.526'	'H66002.526'	'H66003.526'	'H66004.526'
> 
> How can I combine cellfun and strfind to find '001.' in the other strings, please?

cellfun(@(IDX) ~isempty(IDX), strfind(TheCell, '.001'))

This will return a logical vector, one per row, true indicating that the 
  row matched. Is that what you were seeking, or were you looking for 
the indices?

Positions = zeros(length(TheCell),1);
T = strfind(TheCell, '.001);
E = cellfun(@isempty, T);
Positions(~E) = cellfun(@(IDX) IDX(1), T(~E));

This will create Positions as 0 for rows that do not contain the string 
and as the index of the first occurance of 001. for the rows that do.
0
Reply Walter 6/2/2010 4:03:48 PM


"bluesaturn[at]kellnerweg.de" <bluesaturn@kellnerweg.de> wrote in message <842533083.265829.1275487979925.JavaMail.root@gallium.mathforum.org>...
> Hi there.
> I have 1x7 cell array with the contents:
> 
> '.'	'..'	'H66000.526'	'H66001.526'	'H66002.526'	'H66003.526'	'H66004.526'
> 
> How can I combine cellfun and strfind to find '001.' in the other strings, please?
> 
> Thank you.

one of the many solutions

     c={'.','..','ab001','ab002','ac001'};
     ix=~cellfun(@isempty,regexp(c,'001'))
%    ix = 0     0     1     0     1

us
0
Reply us 6/2/2010 4:18:04 PM

The solution proposed by us 
str = '001';
c={'.','..','ab001','ab002','ac001'};
ix=~cellfun(@isempty,regexp(c,str))
I find very elegant. Unfortunately does it only work on char arrays. Can this be modified such that it works for cell arrays of strings as well?

The output then would be a cell array with the same amount of elements as XX. So the statement I am looking for should behave like
str = {'001','b0'};
c={'.','..','ab001','ab002','ac001'};
ix=~cellfun(@isempty,regexp(c,str))

ix{1} = [0 0 1 0 0];
ix{2} = [0 0 1 1 0];

Any ideas?
0
Reply Emil 7/27/2010 7:40:08 AM

"Emil " <emil4ml@gmail.com> wrote in message <i2m2go$ie2$1@fred.mathworks.com>...
> The solution proposed by us 
> str = '001';
> c={'.','..','ab001','ab002','ac001'};
> ix=~cellfun(@isempty,regexp(c,str))
> I find very elegant. Unfortunately does it only work on char arrays. Can this be modified such that it works for cell arrays of strings as well?
> 
> The output then would be a cell array with the same amount of elements as XX. So the statement I am looking for should behave like
> str = {'001','b0'};
> c={'.','..','ab001','ab002','ac001'};
> ix=~cellfun(@isempty,regexp(c,str))
> 
> ix{1} = [0 0 1 0 0];
> ix{2} = [0 0 1 1 0];
> 
> Any ideas?

one of the (more contrived) solutions

     str={'001','b0'};
     c={'.','..','ab001','ab002','ac001'};
     ix=arrayfun(@(x) ~cellfun(@isempty,regexp(c,str(x))),1:numel(str),'uni',false);
     ix=cat(1,ix{:})
%{
%    ix =
     0     0     1     0     1
     0     0     1     1     0
%}

us
0
Reply us 7/27/2010 10:05:08 AM

Thanks, with the help your post in the other thread I came up with the same solution. It looks really elegant! However, when I compared the performance to that of a for-loop I found

tic
for i = 1:n
    for j = 1:numel(id)
         ix{j} = ~cellfun(@isempty,regexp(c,str{j})); 
    end
end
toc

to be 2.5 times faster. I went on to check other examples and found arrayfun to always be slower than the for-loop it is replacing. When is it, performance-wise, an advantage to use arrayfun?
0
Reply Emil 7/28/2010 6:52:06 AM

"Emil " <emil4ml@gmail.com> wrote in message <i2ok2m$75q$1@fred.mathworks.com>...
> Thanks, with the help your post in the other thread I came up with the same solution. It looks really elegant! However, when I compared the performance to that of a for-loop I found
> 
> tic
> for i = 1:n
>     for j = 1:numel(id)
>          ix{j} = ~cellfun(@isempty,regexp(c,str{j})); 
>     end
> end
> toc
> 
> to be 2.5 times faster. I went on to check other examples and found arrayfun to always be slower than the for-loop it is replacing. When is it, performance-wise, an advantage to use arrayfun?

Never.
Only cellfun has 3 syntaxes (the last  3 ones listed in the help) which may be at least as fast as a loop.

Oleg
0
Reply Oleg 7/28/2010 7:41:04 AM

"Emil " <emil4ml@gmail.com> wrote in message <i2ok2m$75q$1@fred.mathworks.com>...
> Thanks, with the help your post in the other thread I came up with the same solution. It looks really elegant! However, when I compared the performance to that of a for-loop I found
> 
> tic
> for i = 1:n
>     for j = 1:numel(id)
>          ix{j} = ~cellfun(@isempty,regexp(c,str{j})); 
>     end
> end
> toc
> 


For even more speed use 'isempty' instead of @isempty.
0
Reply Matt 7/28/2010 3:21:05 PM

Thanks for your inputs. My code works somewhat faster now. So can I conclude that the take-home message here is that for-loops actually are the way to go at times in terms of performance?
0
Reply Emil 7/28/2010 9:36:06 PM

"Emil " <emil4ml@gmail.com> wrote in message <i2q7s6$bqo$1@fred.mathworks.com>...
> Thanks for your inputs. My code works somewhat faster now. So can I conclude that the take-home message here is that for-loops actually are the way to go at times in terms of performance?


.... as has been shown many(!) times on this newsgroup.
0
Reply Matt 7/28/2010 9:48:08 PM

"Emil " <emil4ml@gmail.com> wrote in message <i2ok2m$75q$1@fred.mathworks.com>...
>When is it, performance-wise, an advantage to use arrayfun?

With few exception, arrayfun is cellfun are deceit features introduced by Mathworks few years ago. They are essentially disguised for-loop. Similar topic has been discussed in the pass:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/253815

Bruno
0
Reply Bruno 7/29/2010 6:25:08 AM

10 Replies
577 Views

(page loaded in 0.115 seconds)

Similiar Articles:













7/19/2012 7:44:21 PM


Reply: