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:
Strings combination in cell positions in a variable column size

Subject: Strings combination in cell positions in a variable column size

From: Gonçalo

Date: 23 Mar, 2010 15:49:05

Message: 1 of 7

Hello!
I have the following problem. Lets say i have the following

%cell array with different column sizes ( as a note, Y2011 -> year 2011)
eg{1,1}='Y2011C1.1';
eg{2,1}='Y2011C1.2';
eg{3,1}='Y2011C1.3';
eg{1,2}='Y2012C2.1';
eg{2,2}='Y2012C2.2';
eg{1,3}='Y2013C3.1';
eg{2,3}='Y2013C3.2';

%column size for each column ( i think its key to know this )
size_col_eg=[3 2 2];

I want to concatenate the different string in the cells choosing only one option per year (column), for example:

First scenario: 'Y2011C1.1 Y2012C2.1 Y2013C3.1'
Second scenario: 'Y2011C1.1 Y2012C2.1 Y2013C3.2'
Third scenario: 'Y2011C1.1 Y2012C2.2 Y2013C3.1'
Fourth scenario: 'Y2011C1.1 Y2012C2.2 Y2013C3.2'
Fifth scenario: 'Y2011C1.2 Y2012C2.1 Y2013C3.1'
And so on...

I've been losing myself in "for" circles ... Any ideas? Thanks!

Subject: Strings combination in cell positions in a variable column size

From: Jan Simon

Date: 23 Mar, 2010 19:02:21

Message: 2 of 7

Dear Gonçalo!

> %cell array with different column sizes ( as a note, Y2011 -> year 2011)
> eg{1,1}='Y2011C1.1';
> eg{2,1}='Y2011C1.2';
> eg{3,1}='Y2011C1.3';
> eg{1,2}='Y2012C2.1';
> eg{2,2}='Y2012C2.2';
> eg{1,3}='Y2013C3.1';
> eg{2,3}='Y2013C3.2';
>
> %column size for each column ( i think its key to know this )
> size_col_eg=[3 2 2];
>
> I want to concatenate the different string in the cells choosing only one option per year (column), for example:
>
> First scenario: 'Y2011C1.1 Y2012C2.1 Y2013C3.1'
> Second scenario: 'Y2011C1.1 Y2012C2.1 Y2013C3.2'
> Third scenario: 'Y2011C1.1 Y2012C2.2 Y2013C3.1'
> Fourth scenario: 'Y2011C1.1 Y2012C2.2 Y2013C3.2'
> Fifth scenario: 'Y2011C1.2 Y2012C2.1 Y2013C3.1'
> And so on...
>
> I've been losing myself in "for" circles ... Any ideas? Thanks!

Please definethe correlation between the cell matrix [eg] and the different "scenarios". It is not clear, what "choosing only one option" means.

Kind regards, Jan

Subject: Strings combination in cell positions in a variable column size

From: us

Date: 23 Mar, 2010 19:54:22

Message: 3 of 7

"Gonçalo " <my_zin@hotmail.com> wrote in message <hoanth$18s$1@fred.mathworks.com>...
> Hello!
> I have the following problem. Lets say i have the following
>
> %cell array with different column sizes ( as a note, Y2011 -> year 2011)
> eg{1,1}='Y2011C1.1';
> eg{2,1}='Y2011C1.2';
> eg{3,1}='Y2011C1.3';
> eg{1,2}='Y2012C2.1';
> eg{2,2}='Y2012C2.2';
> eg{1,3}='Y2013C3.1';
> eg{2,3}='Y2013C3.2';
>
> %column size for each column ( i think its key to know this )
> size_col_eg=[3 2 2];
>
> I want to concatenate the different string in the cells choosing only one option per year (column), for example:
>
> First scenario: 'Y2011C1.1 Y2012C2.1 Y2013C3.1'
> Second scenario: 'Y2011C1.1 Y2012C2.1 Y2013C3.2'
> Third scenario: 'Y2011C1.1 Y2012C2.2 Y2013C3.1'
> Fourth scenario: 'Y2011C1.1 Y2012C2.2 Y2013C3.2'
> Fifth scenario: 'Y2011C1.2 Y2012C2.1 Y2013C3.1'
> And so on...
>
> I've been losing myself in "for" circles ... Any ideas? Thanks!

one of the many solutions

% the data
     d={
          'Y2011C1.1' 'Y2012C2.1' 'Y2013C3.1'
          'Y2011C1.2' 'Y2012C2.2' 'Y2013C3.2'
          'Y2011C1.3' [] []
     };
% the engine
     ns=sum(~cellfun(@isempty,d));
     nn=numel(ns);
     ix=cell(1,nn);
     ns=arrayfun(@(x) 1:x,ns,'uni',false);
     [ix{1:nn}]=ndgrid(ns{:});
     ix=reshape(cat(nn+1,ix{:}),[],nn);
     r=cellfun(@(x) {[d{x(1),1},' ',d{x(2),2},' ',d{x(3),3}]},num2cell(ix,2));
% the result
     disp(r);
%{
     'Y2011C1.1 Y2012C2.1 Y2013C3.1'
     'Y2011C1.2 Y2012C2.1 Y2013C3.1'
     'Y2011C1.3 Y2012C2.1 Y2013C3.1'
     'Y2011C1.1 Y2012C2.2 Y2013C3.1'
     'Y2011C1.2 Y2012C2.2 Y2013C3.1'
     'Y2011C1.3 Y2012C2.2 Y2013C3.1'
     'Y2011C1.1 Y2012C2.1 Y2013C3.2'
     'Y2011C1.2 Y2012C2.1 Y2013C3.2'
     'Y2011C1.3 Y2012C2.1 Y2013C3.2'
     'Y2011C1.1 Y2012C2.2 Y2013C3.2'
     'Y2011C1.2 Y2012C2.2 Y2013C3.2'
     'Y2011C1.3 Y2012C2.2 Y2013C3.2'
%}

us

Subject: Strings combination in cell positions in a variable column size

From: Gonçalo

Date: 24 Mar, 2010 14:08:02

Message: 4 of 7

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hob37t$m9c$1@fred.mathworks.com>...
> Dear Gonçalo!
>
> > %cell array with different column sizes ( as a note, Y2011 -> year 2011)
> > eg{1,1}='Y2011C1.1';
> > eg{2,1}='Y2011C1.2';
> > eg{3,1}='Y2011C1.3';
> > eg{1,2}='Y2012C2.1';
> > eg{2,2}='Y2012C2.2';
> > eg{1,3}='Y2013C3.1';
> > eg{2,3}='Y2013C3.2';
> >
> > %column size for each column ( i think its key to know this )
> > size_col_eg=[3 2 2];
> >
> > I want to concatenate the different string in the cells choosing only one option per year (column), for example:
> >
> > First scenario: 'Y2011C1.1 Y2012C2.1 Y2013C3.1'
> > Second scenario: 'Y2011C1.1 Y2012C2.1 Y2013C3.2'
> > Third scenario: 'Y2011C1.1 Y2012C2.2 Y2013C3.1'
> > Fourth scenario: 'Y2011C1.1 Y2012C2.2 Y2013C3.2'
> > Fifth scenario: 'Y2011C1.2 Y2012C2.1 Y2013C3.1'
> > And so on...
> >
> > I've been losing myself in "for" circles ... Any ideas? Thanks!
>
> Please define the correlation between the cell matrix [eg] and the different "scenarios". It is not clear, what "choosing only one option" means.
>
> Kind regards, Jan

to jan:
"Please define the correlation between the cell matrix [eg] and the different "scenarios". It is not clear, what "choosing only one option" means."

Sorry if it wasn't clear. What i meant was that only one value of each column is selected to concatenate the string. I described the scenarios as an attempt to clarify my desired results. :)


to us:
Thanks! Any suggestion if the number of columns in 'd' is variable?
It seams a bit inefficient if a do a "switch/case" or "if" structure to cover a range of column sizes...

The problem is this line:

r=cellfun(@(x) {[d{x(1),1},' ',d{x(2),2},' ',d{x(3),3}]},num2cell(ix,2));

Any suggestion if the number of columns is also variable?

Thanks for all the answers!

Subject: Strings combination in cell positions in a variable column size

From: us

Date: 24 Mar, 2010 15:09:05

Message: 5 of 7

"Gonçalo "
> The problem is this line:
>
> r=cellfun(@(x) {[d{x(1),1},' ',d{x(2),2},' ',d{x(3),3}]},num2cell(ix,2));
>
> Any suggestion if the number of columns is also variable?
>
> Thanks for all the answers!

one of the solutions

% old
     r=cellfun(@(x) {[d{x(1),1},' ',d{x(2),2},' ',d{x(3),3}]},num2cell(ix,2));
% new
     r=deblank(cellfun(@(x) sprintf('%s ',d{x}),num2cell(ix,2),'uni',false));
     disp(r);
%{
     'Y2011C1.1 Y2011C1.1 Y2011C1.1'
     'Y2011C1.2 Y2011C1.1 Y2011C1.1'
     'Y2011C1.3 Y2011C1.1 Y2011C1.1'
     'Y2011C1.1 Y2011C1.2 Y2011C1.1'
     'Y2011C1.2 Y2011C1.2 Y2011C1.1'
     'Y2011C1.3 Y2011C1.2 Y2011C1.1'
     'Y2011C1.1 Y2011C1.1 Y2011C1.2'
     'Y2011C1.2 Y2011C1.1 Y2011C1.2'
     'Y2011C1.3 Y2011C1.1 Y2011C1.2'
     'Y2011C1.1 Y2011C1.2 Y2011C1.2'
     'Y2011C1.2 Y2011C1.2 Y2011C1.2'
     'Y2011C1.3 Y2011C1.2 Y2011C1.2'
%}

us

Subject: Strings combination in cell positions in a variable column size

From: us

Date: 24 Mar, 2010 15:24:05

Message: 6 of 7

"us "
> % new
> r=deblank(cellfun(@(x) sprintf('%s ',d{x}),num2cell(ix,2),'uni',false));

sorry for this UTTER NONSENSE(!)...

one of the solutions

% the data
     d={
          'Y2011C1.1' 'Y2012C2.1' 'Y2013C3.1'
          'Y2011C1.2' 'Y2012C2.2' 'Y2013C3.2'
          'Y2011C1.3' [] []
     };
% the engine
     ns=sum(~cellfun(@isempty,d));
     nn=numel(ns);
     ix=cell(1,nn);
     ns=arrayfun(@(x) 1:x,ns,'uni',false);
     [ix{1:nn}]=ndgrid(ns{:});
     ix=reshape(cat(nn+1,ix{:}),[],nn);
     ds=size(d);
     fh=@(x) sprintf('%s ',d{sub2ind(ds,x,1:ds(2))});
     r=deblank(cellfun(@(x) fh(x),num2cell(ix,2),'uni',false));
% the result
     disp(r);
%{
     'Y2011C1.1 Y2012C2.1 Y2013C3.1'
     'Y2011C1.2 Y2012C2.1 Y2013C3.1'
     'Y2011C1.3 Y2012C2.1 Y2013C3.1'
     'Y2011C1.1 Y2012C2.2 Y2013C3.1'
     'Y2011C1.2 Y2012C2.2 Y2013C3.1'
     'Y2011C1.3 Y2012C2.2 Y2013C3.1'
     'Y2011C1.1 Y2012C2.1 Y2013C3.2'
     'Y2011C1.2 Y2012C2.1 Y2013C3.2'
     'Y2011C1.3 Y2012C2.1 Y2013C3.2'
     'Y2011C1.1 Y2012C2.2 Y2013C3.2'
     'Y2011C1.2 Y2012C2.2 Y2013C3.2'
     'Y2011C1.3 Y2012C2.2 Y2013C3.2'
%}

us

Subject: Strings combination in cell positions in a variable column size

From: Gonçalo

Date: 24 Mar, 2010 15:53:06

Message: 7 of 7

Thank you "us"! Worked perfectly!

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