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:
Another Array of Struct Question ...

Subject: Another Array of Struct Question ...

From: LP Pakula

Date: 11 Mar, 2011 02:19:05

Message: 1 of 5

Hi

i've read Loren's tutorials and found some workarounds but its very slow.

Problem: I have an array of 'uniform' structs that will contain multi-dimensional sub fields

e.g. a(10).covar = rand(2,2);

size(a) => 10x1
size(a.covar) => 2x2

and i want a method such that i get a (10x2x2 ) array back.

Unfortunatly, x = [a.covar] returns a 2x20 ? a 'reshape' just disorignaizes the data.

Thanks, lp

Subject: Another Array of Struct Question ...

From: Ashish Uthama

Date: 11 Mar, 2011 14:26:20

Message: 2 of 5

LP Pakula wrote:
> Hi
>
> i've read Loren's tutorials and found some workarounds but its very slow.
>
> Problem: I have an array of 'uniform' structs that will contain
> multi-dimensional sub fields
>
> e.g. a(10).covar = rand(2,2);
>
> size(a) => 10x1
> size(a.covar) => 2x2
>
> and i want a method such that i get a (10x2x2 ) array back.
>
> Unfortunatly, x = [a.covar] returns a 2x20 ? a 'reshape' just
> disorignaizes the data.
> Thanks, lp

Would 2x2x10* work? (replaced by 3 below)

 >> a(1).covar=ones(2,2);
 >> a(2).covar=2*ones(2,2);
 >> a(3).covar=3*ones(2,2);
 >> collate = reshape([a.covar],[size(a(1).covar) length(a)])

collate(:,:,1) =

      1 1
      1 1


collate(:,:,2) =

      2 2
      2 2


collate(:,:,3) =

      3 3
      3 3

 >> whos collate
   Name Size Bytes Class Attributes

   collate 2x2x3 96 double


If you really need 3x2x2:
permute(collate,[3 1 2 ])

Subject: Another Array of Struct Question ...

From: LP

Date: 12 Mar, 2011 04:49:04

Message: 3 of 5

Ashish Uthama <first.last@mathworks.com> wrote in message <ildbec$7es$1@fred.mathworks.com>...
> LP Pakula wrote:
> > Hi
> >
> > i've read Loren's tutorials and found some workarounds but its very slow.
> >
> > Problem: I have an array of 'uniform' structs that will contain
> > multi-dimensional sub fields
> >
> > e.g. a(10).covar = rand(2,2);
> >
> > size(a) => 10x1
> > size(a.covar) => 2x2
> >
> > and i want a method such that i get a (10x2x2 ) array back.
> >
> > Unfortunatly, x = [a.covar] returns a 2x20 ? a 'reshape' just
> > disorignaizes the data.
> > Thanks, lp
>
> Would 2x2x10* work? (replaced by 3 below)
>
> >> a(1).covar=ones(2,2);
> >> a(2).covar=2*ones(2,2);
> >> a(3).covar=3*ones(2,2);
> >> collate = reshape([a.covar],[size(a(1).covar) length(a)])
>
> collate(:,:,1) =
>
> 1 1
> 1 1
>
>
> collate(:,:,2) =
>
> 2 2
> 2 2
>
>
> collate(:,:,3) =
>
> 3 3
> 3 3
>
> >> whos collate
> Name Size Bytes Class Attributes
>
> collate 2x2x3 96 double
>
>
> If you really need 3x2x2:
> permute(collate,[3 1 2 ])

Subject: Another Array of Struct Question ...

From: LP

Date: 12 Mar, 2011 04:59:24

Message: 4 of 5

Hi Ashish,

thanks for your help that worked!

don;t suppose you know how to go backwards in a vector way?

eg 10x2x2 => struct of legnth 10 with 2x2's in them?

cheers, lyle


Ashish Uthama <first.last@mathworks.com> wrote in message <ildbec$7es$1@fred.mathworks.com>...
> LP Pakula wrote:
> > Hi
> >
> > i've read Loren's tutorials and found some workarounds but its very slow.
> >
> > Problem: I have an array of 'uniform' structs that will contain
> > multi-dimensional sub fields
> >
> > e.g. a(10).covar = rand(2,2);
> >
> > size(a) => 10x1
> > size(a.covar) => 2x2
> >
> > and i want a method such that i get a (10x2x2 ) array back.
> >
> > Unfortunatly, x = [a.covar] returns a 2x20 ? a 'reshape' just
> > disorignaizes the data.
> > Thanks, lp
>
> Would 2x2x10* work? (replaced by 3 below)
>
> >> a(1).covar=ones(2,2);
> >> a(2).covar=2*ones(2,2);
> >> a(3).covar=3*ones(2,2);
> >> collate = reshape([a.covar],[size(a(1).covar) length(a)])
>
> collate(:,:,1) =
>
> 1 1
> 1 1
>
>
> collate(:,:,2) =
>
> 2 2
> 2 2
>
>
> collate(:,:,3) =
>
> 3 3
> 3 3
>
> >> whos collate
> Name Size Bytes Class Attributes
>
> collate 2x2x3 96 double
>
>
> If you really need 3x2x2:
> permute(collate,[3 1 2 ])

Subject: Another Array of Struct Question ...

From: LP

Date: 14 Mar, 2011 00:39:05

Message: 5 of 5

Thought i would post the function i now use... not that using cells is VERY slow, any ideas?

function stOut = arrayOfStructToStructofArrays(st)
% presume a 'uniform' structure

names = fieldnames(st);
for i=1:numel(names);
    switch(class(st(1).(names{i})))
        case 'cell'
            % *** special case for slow cell concatenation
            % optimise with for loop (~2xfaster, who would of thought)
            sz = [length(st), size(st(1).(names{i}))];
            tmp = cell(sz);
            
            % dynamic number of dimensions insertion
            ind = cell(numel(sz),1);
            ind(:) = {':'};
            
            for j=1:length(st)
                ind(1) = {j};
                tmp(ind{:}) = st(j).(names{i});
            end
            stOut.(names{i}) = tmp;
        otherwise
            stOut.(names{i}) = reshape([st.(names{i})],[size(st(1).(names{i})), length(st)]);
            nd = ndims(stOut.(names{i}));
            stOut.(names{i}) = permute(stOut.(names{i}),circshift(1:nd,[0,1]));
    end
end

end

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