ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 25 Feb 2013 00:20:41 +0100Saving data to an array in sagehttps://ask.sagemath.org/question/9833/saving-data-to-an-array-in-sage/Hi,
How can I save data to an array in sage. For example in MATLAB we car make an array of data like this
a =0;
for i=1:10
a = a + 1;
b(i,:) = a
end
Similarlay how can I save data in 2D array.
ThanksFri, 22 Feb 2013 08:13:52 +0100https://ask.sagemath.org/question/9833/saving-data-to-an-array-in-sage/Answer by assadabbasi for <p>Hi,</p>
<p>How can I save data to an array in sage. For example in MATLAB we car make an array of data like this</p>
<p>a =0;</p>
<p>for i=1:10</p>
<p>a = a + 1;</p>
<p>b(i,:) = a</p>
<p>end</p>
<p>Similarlay how can I save data in 2D array.</p>
<p>Thanks</p>
https://ask.sagemath.org/question/9833/saving-data-to-an-array-in-sage/?answer=14593#post-id-14593Thank you very much @calc314Mon, 25 Feb 2013 00:20:41 +0100https://ask.sagemath.org/question/9833/saving-data-to-an-array-in-sage/?answer=14593#post-id-14593Answer by calc314 for <p>Hi,</p>
<p>How can I save data to an array in sage. For example in MATLAB we car make an array of data like this</p>
<p>a =0;</p>
<p>for i=1:10</p>
<p>a = a + 1;</p>
<p>b(i,:) = a</p>
<p>end</p>
<p>Similarlay how can I save data in 2D array.</p>
<p>Thanks</p>
https://ask.sagemath.org/question/9833/saving-data-to-an-array-in-sage/?answer=14577#post-id-14577I don't know here whether you would prefer a list or a matrix or an array as your final data type. But, here is one solution that first builds a list, in case you'd rather have a list. I'm sure there are many other more efficient solutions depending on what you want in the larger picture of what you are doing.
a=0
b=[]
for i in [1..10]:
a+=1
b.append(a)
matrix(b)
Fri, 22 Feb 2013 09:57:09 +0100https://ask.sagemath.org/question/9833/saving-data-to-an-array-in-sage/?answer=14577#post-id-14577Answer by assadabbasi for <p>Hi,</p>
<p>How can I save data to an array in sage. For example in MATLAB we car make an array of data like this</p>
<p>a =0;</p>
<p>for i=1:10</p>
<p>a = a + 1;</p>
<p>b(i,:) = a</p>
<p>end</p>
<p>Similarlay how can I save data in 2D array.</p>
<p>Thanks</p>
https://ask.sagemath.org/question/9833/saving-data-to-an-array-in-sage/?answer=14578#post-id-14578Thanks for the answer. Suppose I want to create a 2D matrix and I have to access its rows or columns or an individual elements. My data type caan be int/float/real number. What can be a solution in this case. An equivalent MATLAB code can be like this.
a = 0
b = 0
c = 0
for i = 1:10
for j = 1:10
b = b +1
c(i,j) = b
end
end
d = c(1,2)
Sorry I am quite new user of sage.Fri, 22 Feb 2013 14:01:27 +0100https://ask.sagemath.org/question/9833/saving-data-to-an-array-in-sage/?answer=14578#post-id-14578Answer by calc314 for <p>Hi,</p>
<p>How can I save data to an array in sage. For example in MATLAB we car make an array of data like this</p>
<p>a =0;</p>
<p>for i=1:10</p>
<p>a = a + 1;</p>
<p>b(i,:) = a</p>
<p>end</p>
<p>Similarlay how can I save data in 2D array.</p>
<p>Thanks</p>
https://ask.sagemath.org/question/9833/saving-data-to-an-array-in-sage/?answer=14572#post-id-14572Here are two approaches.
Building each row as a list...
b=0
c=[]
for i in [1..10]:
tmp=[]
for j in [1..10]:
b+=1
tmp.append(b)
c.append(tmp)
d=matrix(c)
print d
d[0,1]
Using nested list comprehensions...
c=[[10*j+i for i in [1..10]] for j in [0..9]]
d=matrix(c)
d
Sat, 23 Feb 2013 16:46:36 +0100https://ask.sagemath.org/question/9833/saving-data-to-an-array-in-sage/?answer=14572#post-id-14572