473,433 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,433 software developers and data experts.

help with slicing/replacing matrix sections.

I see a more complicated thread on a similar sounding question, but my
question is simpler, I hope.

I have a large numpy matrix, initially created as:

Mat = zeros((a,b), int)

and a smaller array with other data

Sub = [1,2,3,4,5],[6,7,8,9,0]

I want to replace a section of Mat matrix with Sub matrix without having to
loop through each cell in each matrix individually.

I thought index/slice assignments, should be able to do that, but I can only
get it to work (as per book examples) with simple arrays. Every syntax
combination I have tried gives one error or another, typically "can't
broadcast an object....", but intuitively it seems there should be a simple
solution.

In short, how do I insert the data in the two (or any number of) rows in
Sub[0:2] into any part of Mat starting at Mat[x,y] without using "for" loops
?
Jan 14 '08 #1
1 4768
Erik Lind wrote:
I see a more complicated thread on a similar sounding question, but my
question is simpler, I hope.
numpy questions are usually answered better on the numpy mailing list.

http://www.scipy.org/Mailing_Lists
I have a large numpy matrix, initially created as:

Mat = zeros((a,b), int)

and a smaller array with other data

Sub = [1,2,3,4,5],[6,7,8,9,0]

I want to replace a section of Mat matrix with Sub matrix without having to
loop through each cell in each matrix individually.

I thought index/slice assignments, should be able to do that, but I can only
get it to work (as per book examples) with simple arrays. Every syntax
combination I have tried gives one error or another, typically "can't
broadcast an object....", but intuitively it seems there should be a simple
solution.

In short, how do I insert the data in the two (or any number of) rows in
Sub[0:2] into any part of Mat starting at Mat[x,y] without using "for" loops
?
In [11]: Sub = array([[1,2,3,4,5],[6,7,8,9,0]])

In [12]: Mat = zeros((10, 10), int)

In [13]: Mat[5:5+2,4:4+5] = Sub

In [14]: Mat
Out[14]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 2, 3, 4, 5, 0],
[0, 0, 0, 0, 6, 7, 8, 9, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jan 14 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rex_chaos | last post by:
Hi all, I am looking for a high-performance container(matrix and vector) for numerical computation. Someone recommended the boost::multi_array. After having looked at the documentation, I know...
7
by: Jim | last post by:
Hi people. I was hoping someone could help me as this is driving me up the wall. I'm trying to write a program that deals with matrix multiplication. The Program uses a couple of typedefined...
7
by: Dan Trowbridge | last post by:
He everyone, I am just getting started with .NET and I am having a porting problem. I get and error in code that lookssomething like this (really stripped down but you get the idea)... class...
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
10
by: Nevets Steprock | last post by:
I'm writing a web program where one of the sections is supposed to output a correlation matrix. The typical correlation matrix looks like this: ..23 ..34 .54 ..76 .44 .28 ..02 .77 ...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
11
by: John Salerno | last post by:
Given: numbers = can someone explain to me why numbers results in ? I thought the first index, whether going forward or backward, was inclusive. And there is no index of 10 in this...
18
by: John Henry | last post by:
If I have a list of say, 10 elements and I need to slice it into irregular size list, I would have to create a bunch of temporary variables and then regroup them afterwords, like: # Just for...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.