Python Forum
Numpy Play with Rows and Columns of a Matix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Numpy Play with Rows and Columns of a Matix
#1
Hello
I created my code as I want and now I just want to change my matrix.
As a result of my code, I have this matrix:

[Image: 642.png]

And here I want to write each column as a rows and after that I want to add them together. As a result I should see this image:

[Image: 648.png]

Here is my code(I just add the related part):
………….. ##I didn't paste here because we have just definition of list
for triplet in itertools.product([0, 1], repeat=6):
    arr = np.array([])
    for i in range(8):
        for l in range(8):
            if l[l] == n[i]:
                arr = np.append(arr,[1])
                
            else:
                arr = np.append(arr,[0])
                
    print(arr)
How can I change my matrix from first image to second image?
Reply
#2
Is there a question coming?
Reply
#3
(Nov-05-2020, 04:14 PM)deanhystad Wrote: Is there a question coming?

I asked how can I change my matrix from first image to second image
Reply
#4
How about a question that provides enough information and detail that somebody could answer it?

What do you mean by this?
Quote:And here I want to write each column as a rows

Is this what you are looking for?
import numpy as np

m = np.array([[1,2,3,4], [4,6,7,8]])
print(m)
print(m.T) # or use np.transpose(m) or m.transpose()
Output:
[[1 2 3 4] [4 6 7 8]] [[1 4] [2 6] [3 7] [4 8]]
If that is what you want to do, what do you mean by this?
Quote:and after that I want to add them together.
The matrix in the first image isn't square (I don't think), so yow can you add an 8x64 matrix an a 64x8 matrix?

And how does any of this relate to the code you provide?
Reply
#5
(Nov-05-2020, 07:55 PM)deanhystad Wrote: How about a question that provides enough information and detail that somebody could answer it?

What do you mean by this?
Quote:And here I want to write each column as a rows

Is this what you are looking for?
import numpy as np

m = np.array([[1,2,3,4], [4,6,7,8]])
print(m)
print(m.T) # or use np.transpose(m) or m.transpose()
Output:
[[1 2 3 4] [4 6 7 8]] [[1 4] [2 6] [3 7] [4 8]]
If that is what you want to do, what do you mean by this?
Quote:and after that I want to add them together.
The matrix in the first image isn't square (I don't think), so yow can you add an 8x64 matrix an a 64x8 matrix?

And how does any of this relate to the code you provide?

Hello, I wanted to send you a PM but I am not allowed
Here is my code actually according to code it should be 64*64 matrix and before your message I also tried transpose method too but It didn't work
for triplet in itertools.product([0, 1], repeat=6):
    a,ap,b,bp,c,cp = triplet[0],triplet[1],triplet[2],triplet[3],triplet[4],triplet[5]
    nlist = [(a,b,c),(a,b,cp),(a,bp,c),(a,bp,cp),(ap,b,c),(ap,b,cp),(ap,bp,c),(ap,bp,cp)]
    arr = np.array([])
    for i in range(8):
       # arr = np.array([])
        for l in range(8):
            if lst[l] == nlist[i]:
                arr = np.append(arr,[1])
                
            else:
                arr = np.append(arr,[0])
                
    arr=arr.reshape((64,1))
    #print(arr)
    array1 = np.append(array1,arr)
    #print(array1.transpose())
    arr=arr[:]
#array1 = np.append(array1,arr)
#array1=array1.transpose()
print(array1)
Here I can create 64 times, 64*1 matrix and I want to add these 64*1 matrix together so that I could have 64*64 matrix. But I could not manage with that. array1 should be 64*64 matrix by arr which is 64*1 matrix
And here arr is ok but when I tried to add to arr inside array1, I cant add 64*1 matrix, I can add 1*64 matrix and I really don't know why
Reply
#6
Don't PM. This all works better when everyone can participate.

Was the problem with transpose that m and m.T reference the same array? If that is a problem, use copy().
import numpy as np

m = []
rows = 64
cols = 64
for r in range(rows):
    m.append([r*cols+c for c in range(cols)])

m = np.array(m)
mT = m.T.copy()  # Get a copy
mT[1][1] = 0
print(mT - m.T)
Reply
#7
(Nov-05-2020, 08:47 PM)deanhystad Wrote: Don't PM. This all works better when everyone can participate.

Was the problem with transpose that m and m.T reference the same array? If that is a problem, use copy().
import numpy as np

m = []
rows = 64
cols = 64
for r in range(rows):
    m.append([r*cols+c for c in range(cols)])

m = np.array(m)
mT = m.T.copy()  # Get a copy
mT[1][1] = 0
print(mT - m.T)

Hello
Thanks for the answer but the problem is that I cannot bring together 64*1 matrices for creating 64*64 matrix. And I have 64*1 matrices but when I add these 64*1 matrices they are becoming 1*64 matrices and this is very weird By the way I already pasted my code. I didnt understand your example.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,160 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,797 Dec-12-2022, 08:22 PM
Last Post: jh67
  Check DataFrames with different sorting in columns and rows Foxyskippy 0 752 Nov-19-2022, 07:49 AM
Last Post: Foxyskippy
  How can I send a .mp3 to play through my mic? ejected 20 19,811 Sep-01-2022, 02:38 AM
Last Post: victormayer
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,599 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  making variables in my columns and rows in python kronhamilton 2 1,575 Oct-31-2021, 10:38 AM
Last Post: snippsat
  rows from sql query need to write to a file as columns sjcsvatt 6 2,331 Oct-09-2021, 12:45 AM
Last Post: snippsat
  Merging spreadsheets with the same columns and extracting rows with matching entries johnbernard 3 8,351 Aug-19-2021, 03:08 PM
Last Post: johnbernard
  Summing up rows and columns plumberpy 3 2,219 Aug-18-2021, 05:46 AM
Last Post: naughtyCat
  Play the next music in a list Pymax 0 1,181 Jul-28-2021, 07:27 PM
Last Post: Pymax

Forum Jump:

User Panel Messages

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