Thread Subject: Efficiency - Cell size changes on each iteration

Subject: Efficiency - Cell size changes on each iteration

From: Barry Shaw

Date: 1 Feb, 2012 11:27:10

Message: 1 of 8

Hi,

I was wondering if anyone could offer any advice. Below is a simplification of my problem.

The number of elements in a and b can change, so it won't always be a case of 3 elements. I want to be able to produce all possible combinations values of a and b, that is, produce e below.

Is there a more efficicant way of doing this than the code I have below, as I realise the size of c and d change at each iteration.

Thanks
Barry

>> a=[1 2 3];
>> b={'a' 'b' 'c'};
>> c={};
>> d={};
>> for x=1:length(a)
for y =1:length(b)
c{end+1}=a(x);
d{end+1}=char(b(y));
end
end
>> e={};e(1,:)=c;e(2,:)=d;
>> e=e'

e =

    [1] 'a'
    [1] 'b'
    [1] 'c'
    [2] 'a'
    [2] 'b'
    [2] 'c'
    [3] 'a'
    [3] 'b'
    [3] 'c'

Subject: Efficiency - Cell size changes on each iteration

From: ImageAnalyst

Date: 1 Feb, 2012 11:41:52

Message: 2 of 8

Say something like
c = cell(length(a)*length(b));
d = cell(length(a)*length(b));
to preallocate space before the loops begin. Then use c(k) inside the
loops where k is a counter.

Subject: Efficiency - Cell size changes on each iteration

From: Barry Shaw

Date: 1 Feb, 2012 11:59:10

Message: 3 of 8

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <108054f5-21ba-4596-bedf-9cae8f180bd5@o9g2000yqa.googlegroups.com>...
> Say something like
> c = cell(length(a)*length(b));
> d = cell(length(a)*length(b));
> to preallocate space before the loops begin. Then use c(k) inside the
> loops where k is a counter.

Thanks for your reply ImageAnalyst. I've already played about with this, however the difficulties I ran into then was if k was my counter running from 1 to 9, how I produced each possible combination. In other words, I had difficulty with producing e based on your sentence "Then use c(k) inside the loops where k is a counter"

Subject: Efficiency - Cell size changes on each iteration

From: Bruno Luong

Date: 1 Feb, 2012 12:27:10

Message: 4 of 8

a=[1 2 3];
b={'a' 'b' 'c'};

[X Y] = meshgrid(1:length(a),1:length(b));
e = [num2cell( a(X(:))); b(Y(:))]'

% Bruno

Subject: Efficiency - Cell size changes on each iteration

From: Barry Shaw

Date: 1 Feb, 2012 15:09:10

Message: 5 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jgbb2t$l0i$1@newscl01ah.mathworks.com>...
> a=[1 2 3];
> b={'a' 'b' 'c'};
>
> [X Y] = meshgrid(1:length(a),1:length(b));
> e = [num2cell( a(X(:))); b(Y(:))]'
>
> % Bruno

This is what I was looking for. Thanks Bruno. appreciated. saved me a lot of time.

And in case anyone is looking at this post in the future in seek of help, this can be generalised using ndgrid, if using more than 2 variables.

Subject: Efficiency - Cell size changes on each iteration

From: ImageAnalyst

Date: 1 Feb, 2012 15:48:15

Message: 6 of 8

On Feb 1, 6:59 am, "Barry Shaw" <barry.s...@hotmail.co.uk> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <108054f5-21ba-4596-bedf-9cae8f180...@o9g2000yqa.googlegroups.com>...
> > Say something like
> > c = cell(length(a)*length(b));
> > d = cell(length(a)*length(b));
> > to preallocate space before the loops begin.  Then use c(k) inside the
> > loops where k is a counter.
>
> Thanks for your reply ImageAnalyst.  I've already played about with this, however the difficulties I ran into then was if k was my counter running from 1 to 9, how I produced each possible combination.  In other words, I had difficulty with producing e based on your sentence "Then use c(k) inside the loops where k is a counter"

----------------------------------------------------------------------------------------------------
Sorry I didn't have time to spell it out for you when I wrote that.
Working with cells is tricky. This is what I was thinking:

a=[1 2 3];
b={'a' 'b' 'c'};
c = cell(length(a)*length(b), 1);
d = cell(length(a)*length(b), 1);
counter = 1;
for x=1:length(a)
for y =1:length(b)
c{counter}=a(x);
d{counter}=char(b(y));
counter = counter + 1;
end
end
c % Display it
d % Display it

e={c;d} % Stitch together.

You might want to consider working with an array of structures. It's
a hell of a lot easier and you never have to figure out whether to use
() or {}.
ImageAnalyst

Subject: Efficiency - Cell size changes on each iteration

From: Barry Shaw

Date: 2 Feb, 2012 10:00:14

Message: 7 of 8

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <df8f8ec0-83fc-4957-8372-321e74217bb2@t15g2000yqi.googlegroups.com>...
> On Feb 1, 6:59 am, "Barry Shaw" <barry.s...@hotmail.co.uk> wrote:
> > ImageAnalyst <imageanal...@mailinator.com> wrote in message <108054f5-21ba-4596-bedf-9cae8f180...@o9g2000yqa.googlegroups.com>...
> > > Say something like
> > > c = cell(length(a)*length(b));
> > > d = cell(length(a)*length(b));
> > > to preallocate space before the loops begin.  Then use c(k) inside the
> > > loops where k is a counter.
> >
> > Thanks for your reply ImageAnalyst.  I've already played about with this, however the difficulties I ran into then was if k was my counter running from 1 to 9, how I produced each possible combination.  In other words, I had difficulty with producing e based on your sentence "Then use c(k) inside the loops where k is a counter"
>
> ----------------------------------------------------------------------------------------------------
> Sorry I didn't have time to spell it out for you when I wrote that.
> Working with cells is tricky. This is what I was thinking:
>
> a=[1 2 3];
> b={'a' 'b' 'c'};
> c = cell(length(a)*length(b), 1);
> d = cell(length(a)*length(b), 1);
> counter = 1;
> for x=1:length(a)
> for y =1:length(b)
> c{counter}=a(x);
> d{counter}=char(b(y));
> counter = counter + 1;
> end
> end
> c % Display it
> d % Display it
>
> e={c;d} % Stitch together.
>
> You might want to consider working with an array of structures. It's
> a hell of a lot easier and you never have to figure out whether to use
> () or {}.
> ImageAnalyst

Thanks for taking time to reply ImageAnalyst.

I'll compare your method with Bruno's above.
Barry

Subject: Efficiency - Cell size changes on each iteration

From: Diederick

Date: 2 Feb, 2012 12:39:11

Message: 8 of 8

"Barry Shaw" wrote in message <jgb7ie$b0e$1@newscl01ah.mathworks.com>...
> The number of elements in a and b can change, so it won't always be a case of 3 elements. I want to be able to produce all possible combinations values of a and b, that is, produce e below.

Try this (store the whole stuff in one m-file)

Best,
Dee
----------
function out = CombVec(varargin)
%CombVec Generate all possible combinations of input vectors.
%
% CombVec(A1,A2,...) takes any number of inputs,
% A1 - Matrix of N1 (column) vectors.
% A2 - Matrix of N2 (column) vectors.
% ...
% and returns a matrix of (N1*N2*...) column vectors, where the columns
% consist of all possibilities of A2 vectors, appended to
% A1 vectors, etc.
%
% Example
%
% a1 = [1 2];
% a2 = [3 4; 3 4];
% a3 = CombVec(a1,a2)
% a3 =
% 1 2 1 2
% 3 3 4 4
% 3 3 4 4

% 2008-08-06 DN Wrote it, modification of CombVec in Matlab's Neural
% Network Toolbox

if isempty(varargin)
    out = [];
else
    out = varargin{1};
    for i=2:length(varargin)
        cur = varargin{i};
        out = [copyb(out,size(cur,2)); copyi(cur,size(out,2))];
    end
end

%=========================================================
function b = copyb(mat,s)

[mr,mc] = size(mat);
inds = 1:mc;
inds = inds(ones(s,1),:).';
b = mat(:,inds(:));
%=========================================================

function b = copyi(mat,s)

[mr,mc] = size(mat);
inds = 1:mc;
inds = inds(ones(s,1),:);
b = mat(:,inds(:));

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
cell size Barry Shaw 1 Feb, 2012 06:29:13
loop iteration Barry Shaw 1 Feb, 2012 06:29:13
preallocating Barry Shaw 1 Feb, 2012 06:29:13
rssFeed for this Thread

Contact us at files@mathworks.com