Python Forum
How to replace column of a Matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to replace column of a Matrix
#1
Hi,

I am wondering how to replace column of one matrix with another matrix column. Here is my code.

import random
import numpy as np
import matplotlib    
import matplotlib.pyplot as plt

# Matrix 
A = [[1, 2, 3, 4], 
    [5, 6, 7, 8],
    [9, 10, 11, 12]]

B = [[0, 0, 0, 0], 
    [0, 0, 0, 0],
    [0, 0, 0, 0]]

# Elements of Matrix 
print("Matrix A =", A) 
print("1nd row " + " A[0] = ", A[0])
print("2nd row " + " A[1] = ", A[1])
print("3rd row " + " A[2] = ", A[2])
print("3rd element of 2nd row " + " A[1][2] = ", A[1][2])   
print("Last element of 1st Row" + " A[0][-1] = ", A[0][-1])   

# Replacing Rows  
B[0] = A[0]
print("Matrix A =", A)
print("Matrix B =", B)
 
# Replacing Cols  
B[:,[0]] = A[:,[0]] 
print("Matrix A =", A)
print("Matrix B =", B)
I get this error " B[:,[0]] = A[:,[0]]
TypeError: list indices must be integers or slices, not tuple"
Reply
#2
# Replacing Cols  
for row_b, row_a in zip(B, A):
    row_b[0] = row_a[0]
Reply
#3
Yes, it works but I don't understand the code. what is this zip function.
Reply
#4
https://docs.python.org/3/library/functions.html#zip Wrote:zip(*iterables)
Make an iterator that aggregates elements from each of the iterables.

Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator. Equivalent to:

def zip(*iterables):
    # zip('ABCD', 'xy') --> Ax By
    sentinel = object()
    iterators = [iter(it) for it in iterables]
    while iterators:
        result = []
        for it in iterators:
            elem = next(it, sentinel)
            if elem is sentinel:
                return
            result.append(elem)
        yield tuple(result)
The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n). This repeats the same iterator n times so that each output tuple has the result of n calls to the iterator. This has the effect of dividing the input into n-length chunks.

zip() should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. If those values are important, use itertools.zip_longest() instead.

zip() in conjunction with the * operator can be used to unzip a list:
Reply
#5
(Aug-22-2021, 10:11 AM)Joni_Engr Wrote: Yes, it works but I don't understand the code. what is this zip function.
Reply
#6
Now I get it. Thanks.
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 825 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,370 May-03-2021, 06:30 AM
Last Post: Gribouillis
  How do I make a symmetric matrix from a column vector? leocsmith 3 3,687 Mar-30-2021, 10:17 AM
Last Post: leocsmith
  Want to replace last column header value shantanu97 0 1,750 Mar-25-2021, 06:35 AM
Last Post: shantanu97
  Search & Replace - Newlines Added After Replace dj99 3 3,403 Jul-22-2018, 01:42 PM
Last Post: buran
  matrix from matrix python numpy array shei7141 1 3,706 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