iterate through vector of struct and multiply

i am trying to use vector iteration to preform the function below. none of the vectors are of a set size so they must be able to preform the iteration and function no matter what the size of the vector. if you are able to find a solution to this problem for me i may have a job for you.

the conditions:
(1) the value of each index of the next layer must be equal to all of the previous layers values multiplied by the val2 of the value of each index of the next layer passed through the function.

(2) must iterate through all layers

(3)must iterate through all val2's

example below of how the itteration should effect the values

1
2
3
4
5
6
7
8
9
10
11
12
13
struct ex{
	double val = 0;
	vector<double> val2;
};
vector<vector<ex>> layers;

layers[1][0].val =0;
//first iteration should look like
layers[1][0].val += Functs[0](layers[0][0].val*layers[0][0].val2[0]);
//second iteration should look like
layers[1][0].val += Functs[0](layers[0][1].val*layers[0][1].val2[0]);
// third iteration should look like
layers[1][0].val += Functs[0](layers[0][2].val*layers[0][2].val2[0]); 

Last edited on
ReaperSoul wrote:
i am trying to iterate through this vector and set the value of layers[1][0].val = layers[0][every layer in layer 0].val * layers[0][every layer in layer 0].val2[0]
so set the first value of the next layer to the all values times there val2 of which the index is the index of the value of the next layer passed through a function.

Uh, can you rewrite this in coherent English please? It'll go a long way ;D (Hint: avoid run-on sentences)

Can you describe the kind of data you're storing abstractly, and why you chose to store it in a triple nested vectors? Where/when is val2 getting populated? This might help someone suggest a different way to store it. Perhaps you could have a map with (z,x) pairs, or stringified "z,x" as keys, and val2 as values.

It also seems strange to me that you're changing layers' next row data while still iterating over layers.

self-contained demo with a main() would help
coherent English isn't the easiest when trying to explain how to iterate through 3 different vectors in different ways and pass it through a function. i will edit the main post to include the main function. and the reason for the triple nested vector is that i need a 2d array for the layers but each val has to have a val2 for each item in the next layer so that's where the third vector comes in
Last edited on
based on what you had before (without involvement of Functions), I made the following with a slight tweak to your column iteration -- you'd previously stopped one element short (this makes sense for row since you're changing next, but not column).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <vector>
#include <map>

using namespace std;

struct Example 
{
    Example(double val, vector<double> ys) :
        val(val),
        ys(ys)
    {        
    }

    double val = 0;
    vector<double> ys;
};

void Show(const vector<vector<Example>>& layers)
{
    for(auto& row : layers)
    {
        for (auto& e : row)
        {
            cout << e.val << ": {";
            for (auto& y : e.ys)
                cout << y << " ";
            cout << "}, ";
        }
        cout << endl;
    }
}

int main()
{
    vector<vector<Example>> layers = 
    {
        { {1.0, {1.1, 2.2, 3.3}}, {2.0, {0.0, 2.9, 4.5}}, {3.0, {6.5, 5.9, 6.6}} },
        { {86.3, {0.2, 0.1, 0.5}}, {23.5, {0.6, 0.7, 0.7}}, {9.4, {0.3, 1.0, 0.5}} },
        { {0.0, {}}, {0.0, {}}, {0.0, {}} }
    };
    Show(layers);

    double valsum;
    for (int r=0; r<layers.size()-1; ++r)
    {
        for (int c=0; c<layers[r].size(); ++c)
        {
            valsum = 0;
            Example& ex = layers[r][c];
            for (auto& y : ex.ys)
            {
                valsum += ex.val * y;
            }
            layers[r+1][c].val = valsum;
        }
    }    

    cout << endl << endl;
    Show(layers);

    return 0;
}

1: {1.1 2.2 3.3 }, 2: {0 2.9 4.5 }, 3: {6.5 5.9 6.6 }, 
86.3: {0.2 0.1 0.5 }, 23.5: {0.6 0.7 0.7 }, 9.4: {0.3 1 0.5 }, 
0: {}, 0: {}, 0: {}, 


1: {1.1 2.2 3.3 }, 2: {0 2.9 4.5 }, 3: {6.5 5.9 6.6 }, 
6.6: {0.2 0.1 0.5 }, 14.8: {0.6 0.7 0.7 }, 57: {0.3 1 0.5 }, 
5.28: {}, 29.6: {}, 102.6: {}, 


If I'm visualizing this correctly, we have a sort of dot product of previous row's val with its array of y values, written to val. For the whole thing to work, we'd just need row0 completely filled out, and subsequent rows (except last) to have their y arrays populated but not necessarily have a val set.

As you can see, it'd be problematic if the vectors were of different sizes -- how would we write to v[r+1][c] if the next row has less elements than current?
Last edited on
the problem that has to be figured out is that of each layer being different sizes if they were all the same size i would have been able to do it myself
okay, you need to decide what you want to happen in these cases -- how should the logic flow if (r+1) is smaller than current row? For example, you could just skip remaining elements in that row and break back to the outer loop (around line 49):
1
2
3
4
5
6
7
8
9
10
11
12
double valsum;
for (int r=0; r<layers.size()-1; ++r)
{
    for (int c=0; c<layers[r].size(); ++c)
    {
        if (c == layers[r+1].size())
            break;

        valsum = 0;
        // ...
    }
}
Last edited on
This looks like some sort of AI problem to me?

would it save some clutter if you poked the weight (val2) into say the first location of the vector and then iterated 1 to n instead of 0 to n-1?


you are right to think this is an AI problem and icy you should not be skipping anything
the value should be set to all of the values in the next layer times their val2 for that position in the second layer
Last edited on
something, somewhere, feels like a design issue / overengineered/ overthunk problem. But I haven't seen enough to be 100% sure this is true.

Im drawing a blank.. would it make more sense, and be easier to handle, if everything were the same size and zero padded?? Unclear... but is that a possible fix to the varying size confusion?
Last edited on
The layers[x][y].val should be the sum of what?

Surely it is not:
1
2
3
4
5
6
7
8
9
10
11
12
double bar(double);
double ex::product() const {
  if val2 is empty, then throw hizzy fit
  return val * val2[0];
}


double sum {};
auto & layer = layers[x-1];
for ( const auto & node : layer ) {
  sum += bar( node.product() );
}

for then you could not only layers[x][y].val = sum;, but the whole layer
1
2
3
4
auto & layer = layers[x];
for ( const auto & node : layer ) {
  node.val = sum;
}

for the y was not used.

i have figured it out some of you came half way but none of you found the right solution i would share the solution but it pertains to a project that is currently not available to be discussed. it is not of great importance but you if you figure it out leave a post i may still have a job for you.
Topic archived. No new replies allowed.