Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
accumulating strings?

Subject: accumulating strings?

From: David Epstein

Date: 9 Sep, 2010 15:08:04

Message: 1 of 5

I start with data
indices = [5,1,5,5,2,1]
strs = {'ad','xyz','ef','gh','acb','cd'}

I want to end up with
{{1,{'cd','xyz'},{2,{'acb'}},{5,{'ad','ef','gh'}}}
Note that each cell of strings is sorted, but, if sorting creates a problem, collecting the strings without sorting would also be very helpful.

Is there a good way to do this without a loop? Of course, I want to do this on larger structures----here indices and strs have length 6. I want to apply this to structures where 6 is replaced by 10^5.

Is John D'Errico's consolidator relevant here?

Thanks for any help
David

Subject: accumulating strings?

From: Sean

Date: 9 Sep, 2010 15:31:21

Message: 2 of 5

"David Epstein" <David.Epstein.spam@remove.warwick.ac.uk> wrote in message <i6at8k$qcl$1@fred.mathworks.com>...
> I start with data
> indices = [5,1,5,5,2,1]
> strs = {'ad','xyz','ef','gh','acb','cd'}
>
> I want to end up with
> {{1,{'cd','xyz'},{2,{'acb'}},{5,{'ad','ef','gh'}}}
> Note that each cell of strings is sorted, but, if sorting creates a problem, collecting the strings without sorting would also be very helpful.
>
> Is there a good way to do this without a loop? Of course, I want to do this on larger structures----here indices and strs have length 6. I want to apply this to structures where 6 is replaced by 10^5.
>
> Is John D'Errico's consolidator relevant here?
>
> Thanks for any help
> David

One way:
%Accumulate strings, and their position
S = accumarray(indices.',[1:numel(indices)].',[],@(x){{unique(indices(x)),{strs{x}}}});
%Get rid of the blank ones
idx = cellfun(@(x)~isempty(x),S);
S(idx)

Subject: accumulating strings?

From: Oleg Komarov

Date: 9 Sep, 2010 15:38:20

Message: 3 of 5

"David Epstein" <David.Epstein.spam@remove.warwick.ac.uk> wrote in message <i6at8k$qcl$1@fred.mathworks.com>...
> I start with data
> indices = [5,1,5,5,2,1]
> strs = {'ad','xyz','ef','gh','acb','cd'}
>
> I want to end up with
> {{1,{'cd','xyz'},{2,{'acb'}},{5,{'ad','ef','gh'}}}
> Note that each cell of strings is sorted, but, if sorting creates a problem, collecting the strings without sorting would also be very helpful.
>
> Is there a good way to do this without a loop? Of course, I want to do this on larger structures----here indices and strs have length 6. I want to apply this to structures where 6 is replaced by 10^5.
>
> Is John D'Errico's consolidator relevant here?
>
> Thanks for any help
> David

Why don't you go for something more readable:
>> [num2cell(unique(indices)); arrayfun(@(x) sort(strs(indices(indices == x))), unique(indices),'un',0)].'
ans =
    [1.00] {1x2 cell}
    [2.00] {1x1 cell}
    [5.00] {1x3 cell}
Oleg

Subject: accumulating strings?

From: Jan Simon

Date: 9 Sep, 2010 15:58:04

Message: 4 of 5

Dear Sean,

> idx = cellfun(@(x)~isempty(x),S);

As far as I can see, S is a cell string. Then this is faster:
  idx = cellfun(@isempty, S);
Or even faster:
  idx = cellfun('isempty', S);

Kind regards, Jan

Subject: accumulating strings?

From: Sean

Date: 9 Sep, 2010 16:02:19

Message: 5 of 5

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i6b06c$6ld$1@fred.mathworks.com>...
> Dear Sean,
>
> > idx = cellfun(@(x)~isempty(x),S);
>
> As far as I can see, S is a cell string. Then this is faster:
> idx = cellfun(@isempty, S);
> Or even faster:
> idx = cellfun('isempty', S);
>
> Kind regards, Jan

Good to know, thanks!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us