Show all combinations by multiplying all the numbers in a matrix

0

Particles of a matrix of three columns and an indefinite number of rows.The numbers of the first and second row can be any number, and the third column is always a column of 1. Let's say that the number of rows is 5, and we have for example the matrix M:

2 2 1
3 4 1
9 8 1
6 6 1
7 2 1

I want to know which are combinations C (number of total elements, number of rows). In this case C (15.5) and the result that come from multiplying the different possible combinations. For example:

Combination1: 2 * 3 * 9 * 6 * 7; and multiplying it gives: 2268

Combinations2: 2 * 4 * 8 * 6; and multiplying it gives: 384

    
asked by Diego 23.09.2018 в 18:37
source

1 answer

1

Well, from what I understand you were wrong to give the second "combination" since it would actually be: 2x4x8x6x2 = 768

The problem itself, is very confusing, you do not clarify if you should multiply the elements among themselves not following a possible order or just multiply according to the column, do not clarify exactly what the function should result in.

Anyway, I do not know if this is going to help you but good:

def C(matriz):
    k=0;
    comb_res=[];
    for k in range(len(matriz[k])-1): # largo de las columnas ignorando la columna de los 1
        sub=[];
        elem=[];
        i=0;
        for i in range(len(matriz)): # largo de las filas
            elem.append(matriz[i][k]); #guardo los elementos de la columna k y la fila i
        r=1;
        l=0;
        sub.append(elem); # agrego los elementos
        for l in range(len(elem)): # multiplico los elementos guardados
            r=elem[l]*r;
        sub.append(r); # agrego el resultado 
        comb_res.append(sub);  # agrego los elementos y el resultado de multiplicar
    return comb_res;

matrizA=[[2,2,1],[3,4,1],[9,8,1],[6,6,1],[7,2,1]];
prueba=C(matrizA);
i=0;
print(prueba)

And it returns:

  

[[[2, 3, 9, 6, 7], 2268], [[2, 4, 8, 6, 2], 768]]

Basically it takes all the first elements of the first, second, ..., nth row and saves them. Then, go through the saved items and multiply them. Then, save the saved items and the result of multiplying them in a vector and so on with all the columns. Then, return the created vector.

    
answered by 23.09.2018 в 22:27