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:
Managing multi-level structure

Subject: Managing multi-level structure

From: Liana

Date: 21 Apr, 2011 04:14:04

Message: 1 of 11

Hello,

There is a structure:
s(a).tess, where a = 1..5 and 'tess' is 2 x 3 array.

I need to reformulate these structure into a single array as follows:
s1tess11 s1tess12 s1tess13
s1tess21 s1tess22 s1tess23
s2tess11 s1tess12 s1tess13
s2tess21 s1tess22 s1tess23
...

s5tess11 s1tess12 s1tess13
s5tess21 s1tess22 s1tess23

Is there any way to do that without changing s(a).tess? Thanks!

Subject: Managing multi-level structure

From: Liana

Date: 21 Apr, 2011 19:07:21

Message: 2 of 11

I wonder if it's possible to do...

"Liana" wrote in message <iooauc$9eh$1@fred.mathworks.com>...
> Hello,
>
> There is a structure:
> s(a).tess, where a = 1..5 and 'tess' is 2 x 3 array.
>
> I need to reformulate these structure into a single array as follows:
> s1tess11 s1tess12 s1tess13
> s1tess21 s1tess22 s1tess23
> s2tess11 s1tess12 s1tess13
> s2tess21 s1tess22 s1tess23
> ...
>
> s5tess11 s1tess12 s1tess13
> s5tess21 s1tess22 s1tess23
>
> Is there any way to do that without changing s(a).tess? Thanks!

Subject: Managing multi-level structure

From: Matt J

Date: 21 Apr, 2011 19:57:03

Message: 3 of 11


Do you mean newArray = vertcat(s.tess)?

Subject: Managing multi-level structure

From: Liana

Date: 21 Apr, 2011 21:26:05

Message: 4 of 11

I should mention one more detail about my question. The thing is that 'tess' contains delaunay triangulation data. Therefore, if I run your command, then I receive the error message: Concatenation is not supported by TriRep.
Sorry for skipping this detail. So, can this problem be solved in some other way?

"Matt J" wrote in message <ioq26e$drr$1@fred.mathworks.com>...
>
> Do you mean newArray = vertcat(s.tess)?

Subject: Managing multi-level structure

From: Matt J

Date: 21 Apr, 2011 21:35:06

Message: 5 of 11

"Liana" wrote in message <ioq7dc$ef5$1@fred.mathworks.com>...
> I should mention one more detail about my question. The thing is that 'tess' contains delaunay triangulation data. Therefore, if I run your command, then I receive the error message: Concatenation is not supported by TriRep.
> Sorry for skipping this detail. So, can this problem be solved in some other way?

n=length(s);
TessDat=cell(n,1);
for ii=1:n
  TessDat{ii}=s(ii).tess;
end

newArray=vertcat(TessDat{:});

Subject: Managing multi-level structure

From: Matt J

Date: 21 Apr, 2011 21:41:06

Message: 6 of 11

"Matt J" wrote in message <ioq7u9$n5p$1@fred.mathworks.com>...
>
> n=length(s);
> TessDat=cell(n,1);
> for ii=1:n
> TessDat{ii}=s(ii).tess;
> end
>
> newArray=vertcat(TessDat{:});
==============================

Forget it, that won't work. If tess is of type TriRep (i.e. not a numeric array) it is not clear how you want to define the new array. Show an example of input and desired output data.

Subject: Managing multi-level structure

From: Liana

Date: 22 Apr, 2011 00:37:04

Message: 7 of 11

Thanks, Matt! Below you can see a code snippet that I wanted to vectorise in some way:
 
tessqty = length(dt(:)); % dt is a structure. tessqty = 15
Point1 = []; Point2 = []; Point3 = [];
for i = 1 : tessqty
   tess = dt(i).tess; % tess is 3D Delaunay Triangulation (6 x 4 array)
   faces = [tess(:,[1 2 3]); tess(:,[2 3 4]); ...
            tess(:,[1 3 4]); tess(:,[1 2 4])];
   Point1 = [Point1; dt(i).tess.X(faces(:,1),1) dt(i).tess.X(faces(:,1),2)...
           dt(i).tess.X(faces(:,1),3)];
   Point2 = [Point2; dt(i).tess.X(faces(:,2),1) dt(i).tess.X(faces(:,2),2)...
           dt(i).tess.X(faces(:,2),3)];
   Point3 = [Point3; dt(i).tess.X(faces(:,3),1) dt(i).tess.X(faces(:,3),2)...
           dt(i).tess.X(faces(:,3),3)];
end

There are also other similar parts of the code. But I will be able to update them myself if you give me an idea of how it can be done.

Thanks!

"Matt J" wrote in message <ioq89h$sib$1@fred.mathworks.com>...
> "Matt J" wrote in message <ioq7u9$n5p$1@fred.mathworks.com>...
> >
> > n=length(s);
> > TessDat=cell(n,1);
> > for ii=1:n
> > TessDat{ii}=s(ii).tess;
> > end
> >
> > newArray=vertcat(TessDat{:});
> ==============================
>
> Forget it, that won't work. If tess is of type TriRep (i.e. not a numeric array) it is not clear how you want to define the new array. Show an example of input and desired output data.

Subject: Managing multi-level structure

From: Matt J

Date: 22 Apr, 2011 16:09:05

Message: 8 of 11

"Liana" wrote in message <ioqijg$dk6$1@fred.mathworks.com>...
> Thanks, Matt! Below you can see a code snippet that I wanted to vectorise in some way:
>
> tessqty = length(dt(:)); % dt is a structure. tessqty = 15
> Point1 = []; Point2 = []; Point3 = [];
> for i = 1 : tessqty
> tess = dt(i).tess; % tess is 3D Delaunay Triangulation (6 x 4 array)
> faces = [tess(:,[1 2 3]); tess(:,[2 3 4]); ...
> tess(:,[1 3 4]); tess(:,[1 2 4])];
> Point1 = [Point1; dt(i).tess.X(faces(:,1),1) dt(i).tess.X(faces(:,1),2)...
> dt(i).tess.X(faces(:,1),3)];
> Point2 = [Point2; dt(i).tess.X(faces(:,2),1) dt(i).tess.X(faces(:,2),2)...
> dt(i).tess.X(faces(:,2),3)];
> Point3 = [Point3; dt(i).tess.X(faces(:,3),1) dt(i).tess.X(faces(:,3),2)...
> dt(i).tess.X(faces(:,3),3)];
> end
>
> There are also other similar parts of the code. But I will be able to update them myself if you give me an idea of how it can be done.
=======================

It seems like much too short a FOR-LOOP for you to care about optimizing this. However, you can imitate what I did in Message #5 and accumulate Point1,2,3 in cell arrays instead of matrices. Then, VERTCAT them at the conclusion of the loop.

Subject: Managing multi-level structure

From: Liana

Date: 22 Apr, 2011 19:43:07

Message: 9 of 11

Ok, thank you. I'll try your variant to see if it works faster.

"Matt J" wrote in message <ios971$oqr$1@fred.mathworks.com>...
> "Liana" wrote in message <ioqijg$dk6$1@fred.mathworks.com>...
> > Thanks, Matt! Below you can see a code snippet that I wanted to vectorise in some way:
> >
> > tessqty = length(dt(:)); % dt is a structure. tessqty = 15
> > Point1 = []; Point2 = []; Point3 = [];
> > for i = 1 : tessqty
> > tess = dt(i).tess; % tess is 3D Delaunay Triangulation (6 x 4 array)
> > faces = [tess(:,[1 2 3]); tess(:,[2 3 4]); ...
> > tess(:,[1 3 4]); tess(:,[1 2 4])];
> > Point1 = [Point1; dt(i).tess.X(faces(:,1),1) dt(i).tess.X(faces(:,1),2)...
> > dt(i).tess.X(faces(:,1),3)];
> > Point2 = [Point2; dt(i).tess.X(faces(:,2),1) dt(i).tess.X(faces(:,2),2)...
> > dt(i).tess.X(faces(:,2),3)];
> > Point3 = [Point3; dt(i).tess.X(faces(:,3),1) dt(i).tess.X(faces(:,3),2)...
> > dt(i).tess.X(faces(:,3),3)];
> > end
> >
> > There are also other similar parts of the code. But I will be able to update them myself if you give me an idea of how it can be done.
> =======================
>
> It seems like much too short a FOR-LOOP for you to care about optimizing this. However, you can imitate what I did in Message #5 and accumulate Point1,2,3 in cell arrays instead of matrices. Then, VERTCAT them at the conclusion of the loop.

Subject: Managing multi-level structure

From: Matt J

Date: 22 Apr, 2011 20:18:04

Message: 10 of 11

"Liana" wrote in message <ioqijg$dk6$1@fred.mathworks.com>...
> Thanks, Matt! Below you can see a code snippet that I wanted to vectorise in some way:
>
> tessqty = length(dt(:)); % dt is a structure. tessqty = 15
> Point1 = []; Point2 = []; Point3 = [];
> for i = 1 : tessqty
> tess = dt(i).tess; % tess is 3D Delaunay Triangulation (6 x 4 array)
> faces = [tess(:,[1 2 3]); tess(:,[2 3 4]); ...
> tess(:,[1 3 4]); tess(:,[1 2 4])];
> Point1 = [Point1; dt(i).tess.X(faces(:,1),1) dt(i).tess.X(faces(:,1),2)...
> dt(i).tess.X(faces(:,1),3)];
> Point2 = [Point2; dt(i).tess.X(faces(:,2),1) dt(i).tess.X(faces(:,2),2)...
> dt(i).tess.X(faces(:,2),3)];
> Point3 = [Point3; dt(i).tess.X(faces(:,3),1) dt(i).tess.X(faces(:,3),2)...
> dt(i).tess.X(faces(:,3),3)];
> end

Note also that expressions like faces(:,j) take time and memory to evaluate. You should therefore try to minimize repeated evaluations of the same indexing expression, especially into a large block of memory. E.g., it looks like you could cut each of these down to 1 occurence by doing

for i = 1 : tessqty
   tess = dt(i).tess; % tess is 3D Delaunay Triangulation (6 x 4 array)

   faces = [tess(:,[1 2 3]); tess(:,[2 3 4]); ...
            tess(:,[1 3 4]); tess(:,[1 2 4])];

   X=tess.X;

   Point1{i} = { X(faces(:,1),:) };
   Point2{i} = { X(faces(:,2),:) };
   Point3{i} = { X(faces(:,3),:) };
end

Subject: Managing multi-level structure

From: Matt J

Date: 22 Apr, 2011 20:26:04

Message: 11 of 11

"Matt J" wrote in message <iosnps$1sq$1@fred.mathworks.com>...
>
> Note also that expressions like faces(:,j) take time and memory to evaluate. You should therefore try to minimize repeated evaluations of the same indexing expression, especially into a large block of memory. E.g., it looks like you could cut each of these down to 1 occurence by doing
============

This applies mainly to large numeric arrays. Not cells and structs, which behave like pointers in a lot of ways.

Tags for this Thread

No tags are associated with 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