tags:

views:

67

answers:

4
+3  Q: 

Mapping Vectors

Is there a good way to map vectors? Here's an example of what I mean:

vec0 = [0,0,0,0,0,0,0,0,0,0,0]
vec1 = [1,4,2,7,3,2]
vec2 = [0,0,0,0,0,0,0,0,0]
vec2 = [7,2,7,9,9,6,1,0,4]
vec4 = [0,0,0,0,0,0]

mainvec =
[0,0,0,0,0,0,0,0,0,0,0,1,4,2,7,3,2,0,0,0,0,0,0,0,0,0,7,2,7,9,9,6,1,0,4,0,0,0,0,0,0]

Lets say mainvec doesn't exist (I'm just showing it to you so you can see the general data structure in mind.

Now say I want mainvec(12) which would be 4. Is there a good way to map the call of these vectors without just stitching them together into a mainvec? I realize I could make a bunch of if statements that test the index of mainvec and I can then offset each call depending on where the call is within one of the vectors, so for instance:

mainvec(12) = vec1(1)

which I could do by:

mainvec(index)
if (index >=13)
    vect1(index-11);

I wonder if there's a concise way of doing this without if statements. Any Ideas?

+1  A: 

I would use a jagged array.

You still have to have a loop, but you can keep the separate vectors without redundancy:

var mainvec = new int[][]{vec0, vec1, vec2, vec3, vec4};

int desiredInd = 12, totalInd = 0, rowInd = 0, result;

while(rowInd < mainvec.Length && (totalInd + mainvec[rowInd].Length) <= desiredInd)
{ 
    totalInd += mainvec[rowInd++].Length;
}

if(rowInd < mainvec.Length && (desiredInd - totalInd) < mainvec[rowInd].Length)
{
  result = mainvec[rowInd][desiredInd - totalInd];
}
Matthew Flaschen
+1  A: 

I would create a class that would receive array of lengths, and have a method to give you Array number and Index inside the array for a given index in the combined list.

It would be wrapped by a class that will get references to the actual arrays and an indexer to bring you to the right element.

Pavel Radzivilovsky
A: 

It looks like your doing basic list concatenation, in which case the Concat function would seem to be the most straight forward way of doing things. In real-live code terms, somethng like:

var vec0 = new[] {0,0,0,0,0,0,0,0,0,0,0};
var vec1 = new[] {1,4,2,7,3,2};
var vec2 = new[] {0,0,0,0,0,0,0,0,0}; 
var vec3 = new[] {7,2,7,9,9,6,1,0,4};
var vec4 = new[] { 0, 0, 0, 0, 0, 0 };

var mainvec = vec0.Concat(vec1).Concat(vec2).Concat(vec3).Concat(vec4).ToList();
mainvec[12] == 1;

I'm not really sure of the context behind what you are wanting to do, so there may be a more direct way of doing things, but based on what you've got, this seems the simplest to me.

ckramer
A: 

Are you looking for something like this?

using System.Collections.Generic;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] vec0 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            int[] vec1 = { 1, 4, 2, 7, 3, 2 };
            int[] vec2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            int[] vec3 = { 7, 2, 7, 9, 9, 6, 1, 0, 4 };
            int[] vec4 = { 0, 0, 0, 0, 0, 0 };
            List<int> temp = new List<int>();
            temp.AddRange(vec0);
            temp.AddRange(vec1);
            temp.AddRange(vec2);
            temp.AddRange(vec3);
            temp.AddRange(vec4);
            int[] mainvec = temp.ToArray();
        }
    }
}
Waleed A.K.