1

I'm not sure if I'm using the right nomenclature, so I'll try to make my question as specific as possible. That said, I imagine this problem comes up all the time, and there are probably several different ways to deal with it.

Let's say I have an array (vector) called main of 1000 random years between 1980 and 2000 and that I want to make 20 separate arrays (vectors) out of it. These arrays would be named array1980, array1981, etc., would also have length 1000 but would contain 1s where the index in the name was equal to the corresponding element in main and 0s elsewhere. In other words:

for(int i=0; i<1000; i++){
    if(main[i]==1980){
       array1980[i]=1;
    } else {
       array1980[i]=0;
    }

Of course, I don't want to have to write twenty of these, so it'd be good if I could create new variable names inside a loop. The problem is that you can't generally assign variable names to expressions with operators, e.g.,

String("array"+ j)=... # returns an error

I'm currently using Matlab the most, but I can also do a little in Java, c++ and python, and I'm trying to get an idea for how people go about solving this problem in general. Ideally, I'd like to be able to manipulate the individual variables (or sub-arrays) in some way that the year remains in the variable name (or array index) to reduce the chance for error and to make things easier to deal with in general.

I'd appreciate any help.

brandizzi
  • 26,083
  • 8
  • 103
  • 158
jefflovejapan
  • 2,047
  • 3
  • 20
  • 34

3 Answers3

3
boolean main[][] = new boolean[1000][20];
for (int i=0; i < 1000; i++) {
    array[i][main[i]-1980] = true;
}

In many cases a map will be a good solution, but here you could use a 2-dim array of booleans, since the size is known before (0-20) and continuous, and numerable.

Some languages will initialize an array of booleans to false for every element, so you would just need to set the values to true, to which main[i] points.

since main[i] returns numbers from 1980 to 2000, 1980-main[i] will return 1980-1980=0 to 2000-1980=20. To find your values, you have to add 1980 to the second index, of course.

brandizzi
  • 26,083
  • 8
  • 103
  • 158
user unknown
  • 35,537
  • 11
  • 75
  • 121
  • +1 - an array of arrays instead of a map of arrays is fine for this situation. – Don Roby Aug 24 '11 at 09:15
  • Thanks. For this particular task I'd rather have the ones and zeros since they'll be operated on directly with matrix operations. I could use a binary type I guess, but they'd get converted to ints at runtime anyway, right? – jefflovejapan Aug 25 '11 at 00:52
2

The general solution to this is to not create variables with dynamic names, but to instead create a map. Exactly how that's done will vary by language.

For Java, it's worth looking at the map section of the Sun collections tutorial for a start.

Don Roby
  • 40,677
  • 6
  • 91
  • 113
1

Don Roby's answer is correct, but i would like to complete it.

You can use maps for this purpose, and it would look something like this:

Map<Integer,ArrayList<Integer>> yearMap = new HashMap<Integer,ArrayList<Integer>>();
yearMap.put(1980,new ArrayList<Integer>());
for (int i = 0; i < 1000; i++){
    yearMap.get(1980).add(0);
}
yearMap.get(1980).set(999,1);
System.out.println(yearMap.get(1980).get(999));

But there is probably a better way to solve the problem that you have. You should not ask how to use X to solve Y, but how to solve Y.

So, what is it, that you are trying to solve?

Blin
  • 698
  • 9
  • 18
  • +1: Esp for "there is probably a better way to solve the problem" – Don Roby Aug 24 '11 at 09:16
  • I'm currently doing data analysis and need to keep track of twenty or so of these vectors to use as dummy variables. I could just put them in the columns of a year dummy array and then concatenate that whole array onto the various arrays I'm working with, but any all-zero vectors would need to be removed beforehand, and I'd need to keep track of which years had been removed. That said, I'm a novice programmer, and my instinct is usually to make a bunch of variables with unique names any time I run into a problem like this, and just wanted to see how people tackle it generally. – jefflovejapan Aug 25 '11 at 00:25
  • I guess my whole question is "how do you keep everything organized when you need to do a bunch of operations with the same data?" (vague, I know). I'm drawn to Don Roby's idea of using maps, since I'd only need to keep track of the keys in the context of the individual analyses (i.e., make a map titled "GMM_analysis" that contained all of my data, parameter estimates, statistics, etc., then make another one titled "2SLS_analysis" that contained all of the same original data, but different parameter estimates, etc. – jefflovejapan Aug 25 '11 at 00:42