Python Forum
Loading an array into a matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loading an array into a matrix
#2
Not sure to figure out exactly what you want to do, but I guess the following code may help you. Note that I think it's better to convert directly the list:

import numpy as np

List = [[3.3480644 , 2.6733143 , 2.3946035 , 1.651863  , 1.4944869  , 1.4064457 , 2.0166957 , 2.7554176 ], 
        [2.6761208 , 2.147566  , 1.2485328 , 0.6944433 , 0.87446946 , 1.1815729 , 1.879112  , 2.3758152 ]]

List2Array = np.asarray(List)       # convert a list into a Numpy array
print(f"List2Array={List2Array}\n")

r, c = np.shape(List2Array)         # r=number of rows and c=number of columns

# to add zeros in front
M1 = np.zeros((r, 1), dtype=np.float32)
InFront = np.hstack((M1, List2Array))
print(f"InFront={InFront}\n")

# to add zeros at the bottom
M2 = np.zeros((1, c), dtype=np.float32)
AtBottom = np.vstack((List2Array, M2))
print(f"AtBottom={AtBottom}\n")

# to sum the array per row
SumPerRow = np.sum(List2Array, axis=1)
print(f"SumPerRow={SumPerRow}\n")

# to sum the array per column
SumPercolumn = np.sum(List2Array, axis=0)
print(f"SumPercolumn={SumPercolumn}\n")

# to eventually sum all terms
SumAllTerms = np.sum(List2Array)
print(f"SumAllTerms={SumAllTerms}")
Output:
List2Array=[[3.3480644 2.6733143 2.3946035 1.651863 1.4944869 1.4064457 2.0166957 2.7554176 ] [2.6761208 2.147566 1.2485328 0.6944433 0.87446946 1.1815729 1.879112 2.3758152 ]] InFront=[[0. 3.3480644 2.6733143 2.3946035 1.651863 1.4944869 1.4064457 2.0166957 2.7554176 ] [0. 2.6761208 2.147566 1.2485328 0.6944433 0.87446946 1.1815729 1.879112 2.3758152 ]] AtBottom=[[3.3480644 2.6733143 2.3946035 1.651863 1.4944869 1.4064457 2.0166957 2.7554176 ] [2.6761208 2.147566 1.2485328 0.6944433 0.87446946 1.1815729 1.879112 2.3758152 ] [0. 0. 0. 0. 0. 0. 0. 0. ]] SumPerRow=[17.7408911 13.07763246] SumPercolumn=[6.0241852 4.8208803 3.6431363 2.3463063 2.36895636 2.5880186 3.8958077 5.1312328 ] SumAllTerms=30.81852356
Reply


Messages In This Thread
Loading an array into a matrix - by Scott - May-31-2022, 08:28 AM
RE: Loading an array into a matrix - by paul18fr - Jun-01-2022, 07:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 6,011 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  Check if two matrix are equal and of not add the matrix to the list quest 3 895 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,416 May-03-2021, 06:30 AM
Last Post: Gribouillis
  matrix from matrix python numpy array shei7141 1 3,747 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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