Skip to content
Prev 378395 / 398502 Next

[FORGED] Newbie Question on R versus Matlab/Octave versus C

On 1/28/2019 7:51 PM, Jeff Newmiller wrote:
I think I've come to the same conclusion. :-)
Please explain further, if you don't mind. My background is not in 
programming, but in analog microchip circuit design (I'm now retired). 
Thus I'm a user of circuit simulators, not a programmer of them. Also, 
I'm running this stuff on my home computers, either Linux or Windows 
machines.
After my failed attempt at using Octave, I realized that most likely the 
main contributing factor was that I was not able to figure out an 
efficient data structure to model one person. But C lent itself 
perfectly to my idea of how to go about programming my simulation. So 
here's a simplified pseudocode sort of example of what I did:

To model a single reproducing woman I used this C construct:

typedef struct woman {
   int isAlive;
   int isPregnant;
   double age;
   . . .
} WOMAN;

Then I allocated memory for a big array of these things, using the C 
malloc() function, which gave me the equivalent of this statement:

WOMAN women[NWOMEN];  /* An array of NWOMEN woman-structs */

After some initialization I set up two loops:

for( j=0; j<numberOfYears; j++) {
   for(i=1; i< numberOfWomen; i++) {
     updateWomen();
   }
}

The function updateWomen() figures out things like whether the woman 
becomes pregnant or gives birth on a given day, dies, etc.

I added other refinements that are not relevant here, such as random 
variations of various parameters, using the GNU Scientific Library 
random number generator functions.

If you can suggest a data construct in R or Octave that does something 
like this, and uses your idea of vectorization, I'd like to hear it. I'd 
like to implement it and compare results with my C implementation.
I don't know the answer to that, but perhaps you can help decide.

Alan