How to extract out a non-continuous matrix from a larger matrix based on a list [closed]

2024/11/15 21:56:08

(this is in python 2.7.13)

If I have matrix A:

a =[[0,1,0,0,0,1],  [4,1,0,3,2,0],  [0,0,1,0,0,0],  [0,1,0,0,1,0],  [0,0,0,0,1,0],  [0,0,0,0,0,0],  
]

and a list of index's b =[0, 1, 3]

I would like to extract a smaller matrix from the matrix a using the index list b as the rows and columns.

So, it would be:

[0] row of matrix a; columns[0,1,3]
[1] row of matrix a; columns[0,1,3]
[3] row of matrix a; columns[0,1,3]

Which would equal, as example:

[[0,1,0], #0 row, 0,1,3 items[4,1,3], #1 row, 0,1,3 items[0,1,0]  #3 row, 0,1,3 items
]
Answer

You can do it by:

a =[[0,1,0,0,0,1],  [4,1,0,3,2,0],  [0,0,1,0,0,0],  [0,1,0,0,1,0],  [0,0,0,0,1,0],  [0,0,0,0,0,0],  
]b = [0, 1, 3]
c = [[a[i][j] for j in b] for i in b]
print(c)
# ===>
# [[0, 1, 0], [4, 1, 3], [0, 1, 0]]

(Anyway, Python2 is no longer supported: link. I recommend you use Python3+NumPy, which supports various matrix operations such as flexible indexing that you want.)

https://en.xdnf.cn/q/120408.html

Related Q&A

Python get info from long complicated string

Im trying to get info from strings that I have parsed. Im trying to get the font size. This is the string Im returning style="fill: rgb(0, 0, 0); fill-opacity: 1; font-family: ProjectStocksFont; f…

How can i ask for user input and use that input for this text summarizer i created? [duplicate]

This question already has answers here:How do I read from stdin?(25 answers)Closed last year.I have created a text summarizer based on a code I found on Github. Im trying to have to script ask for the…

Return to main menu function for python calculator program

I was asked to write a calculator in Python, and I completed it, but there is only one issue that needs to be fixed. What is the prerequisite? "Once the user inputs/selects an arithmetic operatio…

what the code to use in # Complete this function with recursion in lines 4

# TODO: 2. Create a function that counts the sum of all the numbers in a list belownumber = [1,2,3,4,5] # Use this list as inputdef hitung_total(listKu):# Complete this function with recursionreturn li…

Pip cannot install some Python packages due to missing platform tag in M1 Mac

Lately I have been facing issues installing packages through pip. For e.g. when I try to install the torch package, it fails with the below error > arch arm64 > python --version 3.10.12 ❯ pip --…

find (i,j) location of closest (long,lat) values in a 2D array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

an error occured to add picture in python

I am new in python and I am using python 3.5 version. I want to add photo to python, and heres the code I wrote:from tkinter import * win=Tk() win.title("Python Image")canvas=Canvas(win,width…

Implementing stack in python by using lists as stacks [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Addition function in Python is not working as expected

I tried to create a function using which I want to do mathematical operations like (Addition and Multiplication), I could able to prompt the values and when I insert the values result is not returning …

Probability Distribution Function Python

I have a set of raw data and I have to identify the distribution of that data. What is the easiest way to plot a probability distribution function? I have tried fitting it in normal distribution. But …