Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping Vectors

Tags:

c#

mapping

vector

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?

like image 207
Dan Snyder Avatar asked Jun 24 '26 16:06

Dan Snyder


2 Answers

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();
        }
    }
}
like image 183
Waleed A.K. Avatar answered Jun 27 '26 06:06

Waleed A.K.


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.

like image 44
Pavel Radzivilovsky Avatar answered Jun 27 '26 06:06

Pavel Radzivilovsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!