help-octave
[Top][All Lists]

## Help filling an array

 From: Rob Teasdale Subject: Help filling an array Date: Sun, 27 May 2007 11:12:40 -0700 (PDT)

```Hi all,

I have been teaching myself GNU Octave over the last week or so by
using an old MATLAB 5 book, the Octave manual and this mailing list. I
have been trying to develop a function that will calculate the area
under a curve at different locations (I am working with blast waves and
need to calculate the areas under the initial and reflected waves). It
is working quite well except that I cannot fill the final array with
results unless I use a for loop. I am sure it can be done better, and I
was hoping that somebody may be able to assist and point me in the
right direction.  I have attached a code snippet below:

% get the min and max limits for trapz

% This bit works as expected - provides the index of the min/max time to
integrate
% indzero is an array of indexes of ALL zero crossing points
% f1 is an array that has the indexes of zero crossing points of interest

limits = [indzero(f1(1:end));indzero(f1(1:end)+1)]'

% Calculate the impulse of each phase and store it in impulse
% t and p are vectors containing the time and pressure information respectively

n = length(limits);

imp = [trapz(t(limits(1:end,1):limits(1:end,2)),
p(limits(1:end,1):limits(1:end,2)))]

% This snippet works, but uses a for loop - but I am sure that I can do it more
efficiently

% even though the limits matrix will generally not have more than 10 rows

for i = 1:n

m = limits(i,1);  % get the min limit index

mx = limits(i,2); % get the max limit index

imp(i,1) = [trapz(t(m:mx),p(m:mx))];

end

I am enjoying getting back into some coding (and besides is more fun than
writing my thesis ), and would love to solve this problem (if it is worth it).
Any help would be appreciated,

Cheers

Rob

___________________________________________________________________________________
How would you spend \$50,000 to create a more sustainable environment in