1

I have matrices of different sizes and I try to increase the value in each row with each iteration.

I managed to increase the value in each row, but when the iteration moves to the next matrix, it only increases the value from the first row of the previous matrix I would need it to proceed from the last row of the matrix

Is the way to save the last iteration from the previous matrix and continue the iteration in the next matrix?

here is the part of the code where I iterate the arrays

c1=c1 + 10 * (np.arange(c1.shape[1]) + 1)*p
p +=1

My result

#first iteration
11 12 13 
21 22 23   
#second iteration
21 22 23 
41 42 43 
61 62 63   
#third iteration    
31 32 33 
61 62 63  

I need

11 12 13 
21 22 23   

31 32 33 
41 42 43 
51 52 53   

61 62 63 
71 72 63

This is my full code where reading input from text file

demofile.txt

>=2 1 2 3 
>=3 1 2 3 
>=2 1 2 3

full code

import os
import sys
import numpy as np
import re

f = open("demofile.txt", "r")
lines = f.readlines()
p=1
#sys.stdout = open("results.txt", "w")
for i in list(lines):
     if i[0] != '<' and i[0] != '>' and i[0] != '=':# change txt file for np.array
        p = str(' '.join(i.split())) 
        print(p)
        
     else:
        w = i[3:]
        frst=i[2]
        w = ', '.join(w.split())
        y = i[2]
        y=int(y)+1
        #INSERT input to array
        c=np.array([w])
        c1 = [int(i) for i in c[0].replace(" ", "").split(",")]

        frst=str(frst).replace("[",'')
        frst=str(frst).replace("]",'')
        frst=int(frst)
        c1=np.array(c1)        

        sest2=c1
        c1=np.array([c1]*frst) #frst is the first value by which the matrix is multiplied
        c1=np.transpose(c1)#transpose
        

        #change value every iteration
        c1=c1 + 10 * (np.arange(c1.shape[1]) + 1)*p
        left=c1*-1
        sedem=np.transpose(left)*-1 #transpose matrix
        p +=1
       
        #editing outputs
        sedem=str(sedem).replace("[",'')
        sedem=str(sedem).replace("]",' 0')
        sedem=str(sedem).replace(".",'')
        sedem = sedem[:-1]
        sedem=str(sedem).replace("\n ",'\n')
        sedem=str(sedem).replace("  ",' ')
        sedem=str(sedem).replace("\n ",'\n')
        sedem=str(sedem).lstrip()       
        print(sedem,'\n')
4
  • what is your input array? Commented Feb 4, 2021 at 14:02
  • the input array is c1, which takes from the demophile the last three numbers in each line and iteratively iterates over each line. In this case, the first array is c1 = np.array ([1,2,3]). This is multiplied by the first target in demofile.txt to resize the array Commented Feb 4, 2021 at 14:09
  • And is your output supposed to be a single 2D array? Commented Feb 4, 2021 at 14:16
  • for each iteration of the 2d array, as it is in the desired output Commented Feb 4, 2021 at 14:35

2 Answers 2

1

Lets try something link this with more usage of numpy to solve the problem. Only use iterator to break the above array into chunks for each input.

You can easily modify your code to get the input from the txt file to a numpy array such that each line in text is a single row in an array.

#Setup each row in text file as a row in a np matrix

arr = np.array([[2,1,2,3],
                [3,1,2,3],
                [2,1,2,3]])

print('INPUT:')
print(arr)
print('')

ones = np.repeat(arr[:,1:], arr[:,0], axis=0)
tens = (np.arange(1,arr[:,0].sum()+1)*10)
nums = ones+tens[:, None]

print('nums:')
print(nums)
print('')

#Splitting nums for each input
l = np.cumsum([0]+list(arr[:,0]))
chunks = [nums[i:j,:] for i,j in zip(l,l[1:])]

print('OUTPUT:')
for i,chunk in enumerate(chunks):
    print('chunk number',i,':')
    print(chunk)
    print('')
INPUT:
[[2 1 2 3]
 [3 1 2 3]
 [2 1 2 3]]

nums:
[[11 12 13]
 [21 22 23]
 [31 32 33]
 [41 42 43]
 [51 52 53]
 [61 62 63]
 [71 72 73]]

OUTPUT:
chunk number 0 :
[[11 12 13]
 [21 22 23]]

chunk number 1 :
[[31 32 33]
 [41 42 43]
 [51 52 53]]

chunk number 2 :
[[61 62 63]
 [71 72 73]]
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

        #change value every iteration
        c1 = np.array([[(p + j) * 10 for j in range(frst)]] * 3) + c1
        left=c1*-1
        sedem=np.transpose(left)*-1 #transpose matrix
        p += frst

You track p stepping by frst and make a matrix of elements which are 10's multiples based on p.

For example, if p == 3, the matrix will be

[[30 40 50]
 [30 40 50]
 [30 40 50]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.