How to Best Use Structures?

How to Best Use Structures?

Post by Raymond T. Josep » Mon, 03 Apr 2000 05:00:00



I have sets of arrays that are used to build matrices.  Each element of
these arrays have parameters that are used in subsequent operations.
Some of the parameters are numeric and others are text.

What is the best way to design and manage such data structures.

Ray

 
 
 

How to Best Use Structures?

Post by Michael Schweisgu » Mon, 03 Apr 2000 05:00:00


this is vaque, but one comment may be to convert the strings to a
numerical value to avoid strcmp overhead.  in fact, you could
generate a custom mfile which only emitted the necessary code
to avoid "if" statements, etc...  besides this, i cant
really picture the process much more.


: I have sets of arrays that are used to build matrices.  Each element of
: these arrays have parameters that are used in subsequent operations.
: Some of the parameters are numeric and others are text.

: What is the best way to design and manage such data structures.

 
 
 

How to Best Use Structures?

Post by Raymond T. Josep » Mon, 03 Apr 2000 05:00:00


Thank you for your response.  I can see that I was very unclear of my
objective.

I have an array of numbers used in matrix operations.  Associated with each
of the elements of the array are groups of parameters.

Specifically, there is an array of nodes and array of edges that describe a
network graph.  The primary attribute of either of these arrays is simply it
index, i.e.. the element number in the array.  Since some matrix operations
require the elements to be reordered, the original index is stored in the
array.  For the edge array, a name is of the edge is placed in one column; a
size is saved, a length, a flow coefficient, etc.
So it would seem that the edge array could be a matrix.  The columns could be
addressed via:
aindex = 1
asize = 2
alength = 3
aflowcoef = 4
Then to multiply a matrix (M) and the array of flow coefficients, it could be
written:
x = M * edge(:,aflowcoef)

But I would prefer to be able to address the array of flow coefficients as
aflowcoef or similar so the matrix operation would look like:
x = M * aflowcoef

I guess this is like an alias; rather than copy the array to a new variable,
just reference with a different name, maybe:
edge.aflowcoef

Ray


> this is vaque, but one comment may be to convert the strings to a
> numerical value to avoid strcmp overhead.  in fact, you could
> generate a custom mfile which only emitted the necessary code
> to avoid "if" statements, etc...  besides this, i cant
> really picture the process much more.


> : I have sets of arrays that are used to build matrices.  Each element of
> : these arrays have parameters that are used in subsequent operations.
> : Some of the parameters are numeric and others are text.

> : What is the best way to design and manage such data structures.

 
 
 

How to Best Use Structures?

Post by Ron van Schynd » Tue, 04 Apr 2000 04:00:00


I have quaternion object array

struct(p)
ans =
1x13 struct array with fields:
    s
    i
    j
    k

that I wish to manipulate.  I have built an appropriate subsasgn/subsref
so that I can access fields in a vectorised manner, such as:

p.s=p.s ./ ones(1,13);

% This line works on the command line, but within QINV, it doesn't

rr=qinv(p);
K? ??? Error using ==> .^
Too many input arguments.


On line 13  ==> q = p.s.^2 + p.i.^2 + p.j.^2 + p.k.^2;

where QINV.M is

function qi = qinv(p)

  % This lot should work but doesn't when p is a vector/array
  %
  p = quat(p);        sz = size(p);
  q = zeros(sz);                           % same shape as p
  q = p.s.^2 + p.i.^2 + p.j.^2 + p.k.^2;          % <===  this line fails
  q(q==0) = NaN;      % quick & dirty....
  p.s = p.s ./ q;          
  p.i = -p.i ./ q;
  p.j = -p.j ./ q;
  p.k = -p.k ./ q;
  qi = p;
  return

%=================================
% working code, but spends >80% of the time in DOUBLE/QUAT conversions

  p = quat(p);        sz = size(p);
  p = p(:);           p2 = double(p);
  q = p2(:,1).^2 + p2(:,2).^2 + p2(:,3).^2 + p2(:,4).^2;
  q(q==0) = NaN;
  p2 = p2 ./ (q*[1 -1 -1 -1]);
  qi = reshape(quat(p2),sz);
  return

------------------------------------------------------

Several question:

1. Is this another occurrence of problem # 23807 from TMW site?

2. Another thing is that I would like MTIMES.M to inherit the matrix
multiplication routines for COMPLEX/DOUBLE but using my TIMES.M, but since
they are not objects, I can't do this.   It would be nice to inherit from the
system objects.

--
Cheers, RON

Quote:>>>>===ICBM====|>     Locn: 37 54'38.8" S   145 07'47.1" E  +/- 50 metres


PhD Student,  <<Insert favourite adjective here>>

MELBOURNE 3168, Vic., AUSTRALIA  http://www.physics.monash.edu.au/~ron/
 
 
 

How to Best Use Structures?

Post by Ron van Schynd » Tue, 04 Apr 2000 04:00:00


(sorry, I hit the wrong button)

I need to use largish arrays (256x256, min) of quaternion data, and so need
to have vectorised functions.  I have implemented a few, but I've come across
a problem which may be linked to #23807 at TMW.

I can only get around it by conversion to QUAT --> DOUBLE --> QUAT, which is
time consuming, or a for loop through each object (which is worse).

I have quaternion object

struct(p)
ans =
1x13 struct array with fields:
    s
    i
    j
    k

that I wish to manipulate.  I have built an appropriate subsasgn/subsref
so that I can access fields in a vectorised manner, such as:

Quote:> p

p(1,1) = 0.0000  < 0.0000, 0.0000, 0.0000 >
p(1,2) = 1.0000  < -0.0000, -0.0000, -0.0000 >
p(1,3) = 0.8660  < 0.2240, 0.2319, 0.3822 >
p(1,4) = -0.5000  < 0.3240, 0.3491, 0.7233 >
p(1,5) = 0.5000  < 0.6653, 0.5337, 0.1505 >
p(1,6) = -0.0000  < -0.1014, -0.6921, -0.7146 >
p(1,7) = -0.8660  < 0.1283, 0.3887, 0.2871 >
p(1,8) = 0.8660  < -0.2641, -0.4056, -0.1255 >
p(1,9) = 0.0000  < 0.5264, 0.4427, 0.7259 >
p(1,10) = -0.5000  < -0.5212, -0.5374, -0.4353 >
p(1,11) = 0.5000  < -0.7972, -0.2931, -0.1689 >
p(1,12) = -0.8660  < -0.0548, -0.3641, -0.3383 >
p(1,13) = -1.0000  < 0.0000, 0.0000, 0.0000 >

Quote:> p.s

ans =
  Columns 1 through 7
         0    1.0000    0.8660   -0.5000    0.5000   -0.0000   -0.8660
  Columns 8 through 13
    0.8660    0.0000   -0.5000    0.5000   -0.8660   -1.0000

Quote:> p.s=p.s ./ ones(1,13);

% The above line works on the command line, but within QINV, a similar line
doesn't

rr=qinv(p);
K? ??? Error using ==> .^
Too many input arguments.


On line 13  ==> q = p.s.^2 + p.i.^2 + p.j.^2 + p.k.^2;

where QINV.M is

-----------------------------------------------------
function qi = qinv(p)

  % This lot should work but doesn't when p is a vector/array
  %
  p = quat(p);        sz = size(p);
  q = zeros(sz);                           % same shape as p
  q = p.s.^2 + p.i.^2 + p.j.^2 + p.k.^2;          % <===  this line fails
  q(q==0) = NaN;      % quick & dirty....
  p.s = p.s ./ q;          
  p.i = -p.i ./ q;
  p.j = -p.j ./ q;
  p.k = -p.k ./ q;
  qi = p;
  return

%=================================
% working code, but spends >80% of the time in DOUBLE/QUAT conversions

  p = quat(p);        sz = size(p);
  p = p(:);           p2 = double(p);
  q = p2(:,1).^2 + p2(:,2).^2 + p2(:,3).^2 + p2(:,4).^2;
  q(q==0) = NaN;
  p2 = p2 ./ (q*[1 -1 -1 -1]);
  qi = reshape(quat(p2),sz);
  return

------------------------------------------------------

Several question:

1. Is this another occurrence of problem # 23807 from TMW site?
and how can I get around it without loop/conversions?

2. Another thing is that I would like MTIMES.M to inherit the matrix
multiplication routines for COMPLEX/DOUBLE but using my TIMES.M, but since
they are not objects, I can't do this.   It would be nice to inherit from the
system objects.

--
Cheers, RON

Quote:>>>>===ICBM====|>     Locn: 37 54'38.8" S   145 07'47.1" E  +/- 50 metres


PhD Student,  <<Insert favourite adjective here>>

MELBOURNE 3168, Vic., AUSTRALIA  http://www.physics.monash.edu.au/~ron/
 
 
 

How to Best Use Structures?

Post by z.. » Wed, 05 Apr 2000 04:00:00


(sorry, I hit the wrong button)

I need to use largish arrays (256x256, min) of quaternion data, and so need
to have vectorised object functions.  I have implemented a few, but I've come
across a problem as shown below which may be linked to #23807 at TMW.

I can only get around it by conversion to QUAT --> DOUBLE --> QUAT, which is
time consuming, or a for loop through each object (which is worse).

Let me explain.  I have quaternion object

struct(p)
ans =
1x13 struct array with fields:
    s
    i
    j
    k

that I wish to manipulate.  I have built an appropriate subsasgn/subsref
so that I can access fields in a vectorised manner, such as:

Quote:> p

p(1,1) = 0.0000  < 0.0000, 0.0000, 0.0000 >
p(1,2) = 1.0000  < -0.0000, -0.0000, -0.0000 >
p(1,3) = 0.8660  < 0.2240, 0.2319, 0.3822 >
p(1,4) = -0.5000  < 0.3240, 0.3491, 0.7233 >
p(1,5) = 0.5000  < 0.6653, 0.5337, 0.1505 >
p(1,6) = -0.0000  < -0.1014, -0.6921, -0.7146 >
p(1,7) = -0.8660  < 0.1283, 0.3887, 0.2871 >
p(1,8) = 0.8660  < -0.2641, -0.4056, -0.1255 >
p(1,9) = 0.0000  < 0.5264, 0.4427, 0.7259 >
p(1,10) = -0.5000  < -0.5212, -0.5374, -0.4353 >
p(1,11) = 0.5000  < -0.7972, -0.2931, -0.1689 >
p(1,12) = -0.8660  < -0.0548, -0.3641, -0.3383 >
p(1,13) = -1.0000  < 0.0000, 0.0000, 0.0000 >

Quote:> p.s

ans =
  Columns 1 through 7
         0    1.0000    0.8660   -0.5000    0.5000   -0.0000   -0.8660
  Columns 8 through 13
    0.8660    0.0000   -0.5000    0.5000   -0.8660   -1.0000

Quote:> p.s=p.s ./ ones(1,13);

% The above line works on the command line, but within QINV, a similar line
doesn't

rr=qinv(p);
K? ??? Error using ==> .^
Too many input arguments.


On line 13  ==> q = p.s.^2 + p.i.^2 + p.j.^2 + p.k.^2;

where QINV.M is

-----------------------------------------------------
function qi = qinv(p)

  % This lot should work but doesn't when p is a vector/array
  %
  p = quat(p);        sz = size(p);
  q = zeros(sz);                           % same shape as p
  q = p.s.^2 + p.i.^2 + p.j.^2 + p.k.^2;          % <===  this line fails
  q(q==0) = NaN;      % quick & dirty....
  p.s = p.s ./ q;          
  p.i = -p.i ./ q;
  p.j = -p.j ./ q;
  p.k = -p.k ./ q;
  qi = p;
  return

%=================================
% working code, but spends >80% of the time in DOUBLE/QUAT conversions

  p = quat(p);        sz = size(p);
  p = p(:);           p2 = double(p);
  q = p2(:,1).^2 + p2(:,2).^2 + p2(:,3).^2 + p2(:,4).^2;
  q(q==0) = NaN;
  p2 = p2 ./ (q*[1 -1 -1 -1]);
  qi = reshape(quat(p2),sz);
  return

------------------------------------------------------

Several question:

1. Is this another occurrence of problem # 23807 from TMW site?
and how can I get around it without loop/conversions?

2. Another thing is that I would like MTIMES.M to inherit the matrix
multiplication routines for COMPLEX/DOUBLE but using my TIMES.M, but since
they are not objects, I can't do this.   It would be nice to inherit from the
system objects.

3. do Subsasgn/subsref only work OUTSIDE of an object's class functions but
not inside it?

--
Cheers, RON

Quote:>>>>===ICBM====|>     Locn: 37 54'38.8" S   145 07'47.1" E  +/- 50 metres


PhD Student,  <<Insert favourite adjective here>>

MELBOURNE 3168, Vic., AUSTRALIA  http://www.physics.monash.edu.au/~ron/
 
 
 

How to Best Use Structures?

Post by Ron van Schynd » Wed, 05 Apr 2000 04:00:00


I have a problem with referencing object members inside the object class
functions in a vectored manner.

I need to use largish arrays (256x256, min) of quaternion data, and so need
to have vectorised object functions.  I have implemented a few, but I've come
across a problem as shown below which may be linked to #23807 at TMW.

I can only get around it by conversion to QUAT --> DOUBLE --> QUAT, which is
time consuming, or a for loop through each object (which is worse).

Let me explain.  I have quaternion object

struct(p)
ans =
1x13 struct array with fields:
    s
    i
    j
    k

that I wish to manipulate.  I have built an appropriate subsasgn/subsref
so that I can access fields in a vectorised manner, such as:

Quote:> p

p(1,1) = 0.0000  < 0.0000, 0.0000, 0.0000 >
p(1,2) = 1.0000  < -0.0000, -0.0000, -0.0000 >
p(1,3) = 0.8660  < 0.2240, 0.2319, 0.3822 >
p(1,4) = -0.5000  < 0.3240, 0.3491, 0.7233 >
p(1,5) = 0.5000  < 0.6653, 0.5337, 0.1505 >
p(1,6) = -0.0000  < -0.1014, -0.6921, -0.7146 >
p(1,7) = -0.8660  < 0.1283, 0.3887, 0.2871 >
p(1,8) = 0.8660  < -0.2641, -0.4056, -0.1255 >
p(1,9) = 0.0000  < 0.5264, 0.4427, 0.7259 >
p(1,10) = -0.5000  < -0.5212, -0.5374, -0.4353 >
p(1,11) = 0.5000  < -0.7972, -0.2931, -0.1689 >
p(1,12) = -0.8660  < -0.0548, -0.3641, -0.3383 >
p(1,13) = -1.0000  < 0.0000, 0.0000, 0.0000 >

Quote:> p.s

ans =
  Columns 1 through 7
         0    1.0000    0.8660   -0.5000    0.5000   -0.0000   -0.8660
  Columns 8 through 13
    0.8660    0.0000   -0.5000    0.5000   -0.8660   -1.0000

Quote:> p.s=p.s ./ ones(1,13);

% The above line works on the command line, but within QINV, a similar line
doesn't

rr=qinv(p);
K? ??? Error using ==> .^
Too many input arguments.


On line 13  ==> q = p.s.^2 + p.i.^2 + p.j.^2 + p.k.^2;

where QINV.M is

-----------------------------------------------------
function qi = qinv(p)

  % This lot should work but doesn't when p is a vector/array
  %
  p = quat(p);        sz = size(p);
  q = zeros(sz);                           % same shape as p
  q = p.s.^2 + p.i.^2 + p.j.^2 + p.k.^2;          % <===  this line fails
  q(q==0) = NaN;      % quick & dirty....
  p.s = p.s ./ q;          
  p.i = -p.i ./ q;
  p.j = -p.j ./ q;
  p.k = -p.k ./ q;
  qi = p;
  return

%=================================
% working code, but spends >80% of the time in DOUBLE/QUAT conversions

  p = quat(p);        sz = size(p);
  p = p(:);           p2 = double(p);
  q = p2(:,1).^2 + p2(:,2).^2 + p2(:,3).^2 + p2(:,4).^2;
  q(q==0) = NaN;
  p2 = p2 ./ (q*[1 -1 -1 -1]);
  qi = reshape(quat(p2),sz);
  return

------------------------------------------------------

Several question:

1. Is this another occurrence of problem # 23807 from TMW site?
and how can I get around it without loop/conversions?

2. Another thing is that I would like MTIMES.M to inherit the matrix
multiplication routines for COMPLEX/DOUBLE but using my TIMES.M, but since
they are not objects, I can't do this.   It would be nice to inherit from the
system objects.

3. do Subsasgn/subsref only work OUTSIDE of an object's class functions but
not inside it?

--
Cheers, RON

Quote:>>>>===ICBM====|>     Locn: 37 54'38.8" S   145 07'47.1" E  +/- 50 metres


PhD Student,  <<Insert favourite adjective here>>

MELBOURNE 3168, Vic., AUSTRALIA  http://www.physics.monash.edu.au/~ron/
 
 
 

How to Best Use Structures?

Post by Michael Schweisgu » Thu, 06 Apr 2000 04:00:00


given that edge is a matrix, than edge(:,aflowcoef) is a vector.

so structures wouldnt be too bad, if the vector has more than 10 to 20
elements.

    edge.aflowcoef = 1:10;

is perfectly legal matlab.  maybe you have:

    edge(2).aflowcoef = 1:10;

for more than one edge.

enjoy structures!


: aflowcoef = 4

: x = M * edge(:,aflowcoef)

: But I would prefer to be able to address the array of flow coefficients as
: aflowcoef or similar so the matrix operation would look like:
: x = M * aflowcoef

: I guess this is like an alias; rather than copy the array to a new variable,
: just reference with a different name, maybe:
: edge.aflowcoef

: Ray

: > this is vaque, but one comment may be to convert the strings to a
: > numerical value to avoid strcmp overhead.  in fact, you could
: > generate a custom mfile which only emitted the necessary code
: > to avoid "if" statements, etc...  besides this, i cant
: > really picture the process much more.
: >

: > : I have sets of arrays that are used to build matrices.  Each element of
: > : these arrays have parameters that are used in subsequent operations.
: > : Some of the parameters are numeric and others are text.
: >
: > : What is the best way to design and manage such data structures.