From: Emil on
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?
From: us on
"Emil " <emil4ml(a)gmail.com> wrote in message <i2m2go$ie2$1(a)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
From: Emil on
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?
From: Oleg Komarov on
"Emil " <emil4ml(a)gmail.com> wrote in message <i2ok2m$75q$1(a)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
From: Matt Fig on
"Emil " <emil4ml(a)gmail.com> wrote in message <i2ok2m$75q$1(a)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.
 |  Next  |  Last
Pages: 1 2
Prev: b2bsharing
Next: Dynamic quantile regression