I am running semiconductor simulations, where semiconductor equations are discretized on a grid. Multiple material parameters are defined at each gridpoint. Ideally I would like to have an array of structures - one element for each gridpoint - containing these material parameters as scalar fields.

I would like an array of structures, because oftentimes I need to do calculations using only a subset of the gridpoints. Using an array of structures, lets call it 's', the function call would simply look like:

result = myFunction(s(10:20));

This is very intuitive to me. Alternatively I could define a structure where the fields are vectors which contain the material parameters at each gridpoint location. However, function calls may then become messy:

result = myFunction(s.A(10:20),s.B(10:20),s.C(10:20),...);

However, I have found that the former approach is much slower. Here is some example code:

function [] = speedTest()

len = 2e4;

m1(len).x = [];

m2.x = zeros(len,1);

for ii=1:len,

x = rand;

m1(ii).x = x;

m2.x(ii) = x;

end

tic

r1 = method1(m1(1:round(len/2)));

toc

tic

r2 = method2(m2.x(1:round(len/2)));

toc

function [result] = method1(m1)

result = exp([m1.x]).*[m1.x];

function [result] = method2(x)

result = exp(x).*x;

Method 1 runs much slower than method 2. This means I either need to write either slower code or code that is more difficult to understand. Is there any way to get the best of both worlds?

This would be a dream!!!

Bruno

1. How to get elements in a structure array according to a structure field value in Matlab?

2. cmp-speed 'array of structure vs. structure of array'

3. assigning a vector to the fields of a structured array

4. cmp-speed 'array of structure vs. structure of

5. structures, structures and more structures (questions about nested structures)

6. How can I transfer field names from an existing array to a structure array?

7. Advice for field structure or query structure - not getting the results I thought I should

8. structure address = structure first field address ?

9. array in structure(the array itself is also a structure)

10. How to initialize a variable as a array structure / structure (type) array (see example)

11. Structure of arrays or arrays of structures?

12. how to marshal an array of unmanaged structure into a managed array of structures.

13. marshal 2-d arrays, structure members, and arrays of structures

14. Convert Array of Structures to Structure of arrays??

15. converting a structure of arrays into an array of structures.

2 post • Page:**1** of **1**