Python Forum

Full Version: Comparing columns of Matrix stored in .txt files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have 2 separate .txt files displaying 2 matrices each:

Matrix A:
1	1.0	0	3851.549175736092	5761.18503664513	0.0	1192.84
2	0.906100.0351229986	-4.561573852089962	0.0	-2.5579538487363607e-12	0.0	0.0
3	0.8933992034203796	-5.284140443354556	0.0	3.865352482534945e-12	0.0	0.0
4	0.9016105506150125	-4.827696564047867	0.0	0.0	723.69	723.69
5	0.8958860109540898	-5.146754745996865	0.0	0.0	117.4	117.4
6	0.9065329176302267	-4.5402187020437115	0.0	0.0	0.0	0.0
7	0.9128962882102003	-4.209844474756017	0.0	0.0	313.48	313.48
8	0.9056610195794378	-4.583294572479561	0.0	0.0	0.0	0.0
9	0.8897263340095604	-5.4890257695280456	0.0	0.0	217.265	217.265
10	0.8964974852271693	-5.100621494129068	0.0	0.0	405.87	405.87
11	0.9066859240794467	-4.531331203775372	0.0	0.0	374.315	374.315
12	0.9063780632661372	-4.546920438080629	0.0	0.0	0.0	0.0
13	0.8808632753328488	-5.975343526352269	0.0	0.0	561.735	561.735
14	0.8927644316076149	-5.274761747583852	0.0	0.0	557.455	557.455
15	0.9295254680662713	-3.368510047897745	0.0	0.0	535.95	535.95
Matrix B:
1	3	000	1192.84	0	0	1	1	0	230	1	1.05	0.95
2	1	0	0	0	0	1	1	0	400	1	1.05	0.95
3	1	0	0	0	0	1	1	0	230	1	1.05	0.95
4	1	723.69	723.690	0	0	1	1	0	230	1	1.05	0.95
5	1	117.40	117.40	0	0	1	1	0	230	1	1.05	0.95
6	1	0	0	0	0	1	1	0	400	1	1.05	0.95
7	1	313.48	313.48	0	0	1	1	0	230	1	1.05	0.95
8	1	0	0	0	0	1	1	0	400	1	1.05	0.95
9	1	217.265	217.265	0	0	1	1	0	230	1	1.05	0.95
10	1	405.870	405.870	0	0	1	1	0	230	1	1.05	0.95
11	1	374.315	374.315	0	0	1	1	0	230	1	1.05	0.95
12	1	0	0	0	0	1	1	0	400	1	1.05	0.95
13	1	561.735	561.735	0	0	1	1	0	230	1	1.05	0.95
14	1	557.455	557.455	0	0	1	1	0	230	1	1.05	0.95
15	1	535.950	535.950	0	0	1	1	0	230	1	1.05	0.95	
As I am new to coding in general, would a kind soul help me with a rough script of the following in python?

I would like to compare the elements of the 2nd column vector of Matrix A with the last 2 columns of Matrix B, with the latter being the max and min values of a constraint (max = 1.05; min = 0.95).

The objective is to write an 'if' loop to check if MatrixA[:,1] falls within the constraints of MatrixB[:,11] and MatrixB[:,12].

In the case of a constraint violation, the script should throw an error message and Matrix B would not be generated.

Else, if MatrixA[;,1] is within the constraints, the script would run and successfully generate Matrix B.

Note: The matrices are dynamically generated by a separate program into .txt files. My main problem is going about pulling the values out from the individual matrix and comparing the values between the 2 .txt files containing Matrix A and B respectively.
Can you please share the code that you have tried?
Thanks very much for your reply! The following is basically what I am trying to implement.
My problem is with writing the code for comparing 2 matrices stored in .txt files.

import numpy as np

matrixA = np.array([
    [1,   1.0,                                   0, 3851.549175736092,        5761.18503664513,    0.0,     1192.84],
    [2,   0.9061000351229986,   -4.561573852089962,               0.0,            -2.5579538487363607e-12, 0.0, 0.0],
    [3,   0.8933992034203796,   -5.284140443354556,               0.0,            3.865352482534945e-12,   0.0, 0.0],
    [4,   0.9016105506150125,   -4.827696564047867,               0.0,    0.0, 723.69, 723.69],
    [5,   0.8958860109540898,   -5.146754745996865,               0.0,    0.0, 117.4,   117.4],
    [6,   0.9065329176302267,  -4.5402187020437115,               0.0,          0.0, 0.0, 0.0],
    [7,   0.9128962882102003,   -4.209844474756017,               0.0,   0.0, 313.48,  313.48],
    [8,   0.9056610195794378,   -4.583294572479561,               0.0,          0.0, 0.0, 0.0],
    [9,   0.8897263340095604,   -5.4890257695280456,              0.0,  0.0, 217.265, 217.265],
    [10,  0.8964974852271693,   -5.100621494129068,               0.0,   0.0, 405.87,  405.87],
    [11,  0.9066859240794467,   -4.531331203775372,               0.0,  0.0, 374.315, 374.315],
    [12,  0.9063780632661372,   -4.546920438080629,               0.0,          0.0, 0.0, 0.0],
    [13,  0.8808632753328488,   -5.975343526352269,               0.0,  0.0, 561.735, 561.735],
    [14,  0.8927644316076149,   -5.274761747583852,               0.0,  0.0, 557.455, 557.455],
    [15,  0.9295254680662713,   -3.368510047897745,               0.0,   0.0, 535.95,  535.95]
    ])


matrixB = np.array([
    [1, 	3,	1192.84,	1192.84,	0,	0,	1,	1,	0,	230,	1,	1.05,	0.95],
    [2, 	1,	      0,	      0,	0,	0,	1,	1,	0,	400,	1,	1.05,	0.95],
    [3, 	1,	      0,	      0,	0,	0,	1,	1,	0,	230,	1,	1.05,	0.95],
    [4,  	1,	 723.69,	723.690,	0,	0,	1,	1,	0,	230,	1,	1.05,	0.95],
    [5, 	1,	 117.40,	 117.40,	0,	0,	1,	1,	0,	230,	1,	1.05,	0.95],
    [6, 	1,	      0,	      0,	0,	0,	1,	1,	0,	400,	1,	1.05,	0.95],
    [7, 	1,	 313.48,	 313.48,	0,	0,	1,	1,	0,	230,	1,	1.05,	0.95],
    [8, 	1,	      0,	      0,	0,	0,	1,	1,	0,	400,	1,	1.05,	0.95],
    [9, 	1,	 217.265,	 217.265,	0,	0,	1,	1,	0,	230,	1,	1.05,	0.95],
    [10,	1,	 405.870,	 405.870,	0,	0,	1,	1,	0,	230,	1,	1.05,	0.95],
    [11,	1,	 374.315,	 374.315,	0,	0,	1,	1,	0,	230,	1,	1.05,	0.95],
    [12,	1,	      0,	      0,	0,	0,	1,	1,	0,	400,	1,	1.05,	0.95],
    [13,	1,	 561.735,	 561.735,	0,	0,	1,	1,	0,	230,	1,	1.05,	0.95],
    [14,	1,	 557.455,	 557.455,	0,	0,	1,	1,	0,	230,	1,	1.05,	0.95],
    [15,	1,	 535.950,	 535.950,	0,	0,	1,	1,	0,	230,	1,	1.05,	0.95]
    ])


rows = len(matrixA[:, 0]) #15 rows


for R in range(rows):

    if matrixA[R, 1] > matrixB[R,11] or matrixA[R, 1] < matrixB[R,12]:
        break
    print("Contraints violated!")

Additionally, how should I write the code to overwrite the contents of matrixA.txt with "Constraints violated!" in the case of the 'if' loop being true.

In the above example, I used values for the 2nd row of Matrix A which are not within the limits of the maximum and minimum values given in rows 12 & 13 of Matrix B

In the case of a non-violation, Matrix A should show the respective matrix which adheres to the limits given.

Thanks very much in advance!