Python Forum
Comparing columns of Matrix stored in .txt files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing columns of Matrix stored in .txt files
#1
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.
Reply
#2
Can you please share the code that you have tried?
Reply
#3
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 823 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Comparing two columns with same value but different font format doug2019 1 721 Jan-08-2023, 02:58 PM
Last Post: Larz60+
  comparing columns arvin 5 1,071 Dec-14-2022, 01:54 PM
Last Post: paul18fr
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,358 May-03-2021, 06:30 AM
Last Post: Gribouillis
  Concatenate two files with different columns into one dataframe moralear27 1 2,126 Sep-11-2020, 10:18 PM
Last Post: moralear27
  best option for comparing two csv files zuzuzu 1 2,127 Apr-15-2020, 05:20 PM
Last Post: Larz60+
  Dividing certain columns in multiple CSV files NatesM 4 4,061 Dec-20-2019, 07:59 PM
Last Post: ichabod801
  Loop files - Extract List Data To Individual Columns in CSV dj99 5 3,270 May-19-2019, 10:29 AM
Last Post: dj99
  Comparing values in large txt files StevenVF 2 2,749 Feb-28-2019, 09:07 AM
Last Post: StevenVF
  Comparing two files data while one got hex and other binary A_Embedded 3 3,719 Apr-07-2018, 09:58 PM
Last Post: A_Embedded

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020