473,322 Members | 1,566 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,322 software developers and data experts.

How can I store a result in a Matrix?

I am new to Python so please excuse me if this sounds simple! Before I
explain what i want to do let me write some code in matlab which
demonstrates what I would like to acheive, using the Fibonnachi series
as an example:
a = 1
b = 1
for i = 1:10
z = a + b
a = b
b = z
y(i) = z
end

This gives me the variable y with values
2 3 5 8 13 21 34 55 89 144

which is essentially a matrix which i can plot against another matrix
xof the same dimensions

In python I have

a = 1
b = 1
for i in range(1, 11)
z = a + b
a = b
b = z

which gives me all the values I need, but i do not know how to store
the values of z in a single matrix. I have looked at several online
resources and played about with the array commands but cant get it to
work. If someone could point me to a relevant webpage or suggest code
that will work I would be very grateful

Thanks

Ben
Jul 18 '05 #1
1 1543
Ben Champion wrote:
I am new to Python so please excuse me if this sounds simple! Before I
explain what i want to do let me write some code in matlab which
demonstrates what I would like to acheive, using the Fibonnachi series
as an example: [snip matlab code]
This gives me the variable y with values
2 3 5 8 13 21 34 55 89 144


If you really want to be dealing with matrices, you should probably
dowload the numarray package:
http://www.stsci.edu/resources/softw...dware/numarray

If a list is sufficient, I would write this something like:
def fib(): .... a, b = 1, 1
.... while True:
.... yield a
.... a, b = b, a + b
.... import itertools as it
list(it.islice(fib(), 2, 12))

[2, 3, 5, 8, 13, 21, 34, 55, 89, 144]

The fib function is a generator that will yield, one at a time, each
value in the fibonacci sequence. Since you only wanted values 2 through
11 in your example, I use itertools.islice to select the appropriate
part of the generated sequence.

Note also that because Python has tuple unpacking, you can do multiple
simulaneous assignment, so there's no need to have that extra temp
variable 'z'.

Steve
Jul 18 '05 #2

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

Similar topics

1
by: Michael Allen | last post by:
Hi all you Java 2D gurus out there. I wonder if you might be able to help me. I've been dipping my toe into the world of Gaussian blurring and with the help of a few articles and some bashing my...
3
by: jacob nikom | last post by:
Hi, I would like to be able to store numerical array in MySQL. I am really looking for the solution to store it in the one field only. For example, I have a matrix, which I want to store in...
12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
2
by: rockyptc | last post by:
greetings. first, i apologize for asking an old question. it appears that i'm looking for a solution that was already given but it don't seem to fit my scenario. so thanks for putting up with me. ...
6
by: n00dle | last post by:
hi, see if i want to copy the contents of a char arrary into a equally size struct, assuming that the structure has no holes, padding etc., i can use structure = *(t_structure *) array; but...
0
by: Pixie Songbook | last post by:
The following for loop (and extra from a much larger program) calculates elements of a matrix for use in newtonian minimisation of a non-lienar model. the problem is it wrongly calculates the value...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.