How to sum a matrice of matrices [closed] - python-2.7

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 5 years ago.
Improve this question
I have a matrix that each spot in it is another matrix, and I need to print a new matrix which is a form from the sum of each matrix in the same index .
>>sum_matrices([[[1,2,3],[4,5,6]],[[11,12,13],[14,15,16]],[[21,22,23],[24,25,26]],[[31,3 2,13],[34,35,36]]])
[[64, 68, 52], [76, 80, 84]]
I have this code which adds 2 matrices together.
def sum_matrices(mat_lst):
result = []
for i in range(len(mat_lst)):
rows=[]
for j in range(len(mat_lst)):
rows.append(add_matrices(mat_lst[i][j]))
result.append(rows)
return result

Try like this,
def sum(matrix):
result = matrix[0][:]
for i in range(1, len(matrix)):
for j in range(len(result)):
for k in range(len(result[j])):
result[j][k] += matrix[i][j][k]
print result

Related

Update series of numeric values in long string [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 2 years ago.
Improve this question
I have text column with following examplary data:
5,5,0.1;6,6,0.15;7,7,0.2;8,8,0.25;9,9,0.3;10,10,0.35;11,11,0.4;12,12,0.45;13,13,0.5;14,14,0.55;15,15,0.6;16,16,0.65;17,17,0.7;18,18,0.75;19,19,0.8;20,20,0.85;
I need to add some fixed value to each of numeric values (the one before semicolon)
so for example from:
5,5,0.1;6,6,0.15; I want add 0.15 so result would be:
5,5,0.25;6,6,0.3;
I guess I should try something with regexp_replace but I have no idea how to start here
The correct solution would be fix your broken data model and not store multiple, delimited values in a single column.
I wouldn't do this with a regex, but unnesting the elements of the string, adding the value to the third element, then aggregate everything back into the broken design:
update badly_designed_table
set denormalized_column =
(select string_agg(concat_ws(',', a, b, round(c + 0.15,2)), ';' order by idx)
from (
select split_part(val, ',', 1) as a,
split_part(val, ',', 2) as b,
split_part(val, ',', 3)::numeric as c,
idx
from unnest(string_to_array(bad_column, ';')) with ordinality as x(val,idx)
-- skip the "empty" element generated by the trailing ;
where nullif(val, '') is not null
) t)

How do i do arithmetic for all elements of a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to find the biggest factor of the number 600851475143
so in order for doing that i want to find all primes smaller that this number
number = input("enter max number:")
def findprime (number):
prime = [1,2]
for i in range (2,number):
if(i%)
how do i preform arithmetic's for all numbers in a list?
To find the largest factor, find the smallest one and divide. And you only need to check up to the sqrt of the number:
factor = 0
for i in range (2, int(number**0.5) + 1):
if number%i == 0:
factor = i
break
if factor: print(number/factor)
else: print number, 'is prime'

Separate value after certain character by space in Swift [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
hi i have a value which is
Blockquote
1 1:0.0644343 1 1:0.0309334 1 1:0.0261616
Blockquote
i want to separate value by space but after certain character to get result like this..is there any possible solution . i know we can do in regex
Blockquote
"1 1:0.0644343"
"1 1:0.0309334"
"1 1:0.0261616"
Blockquote
I think regex is the perfect tool here:
var str = "Blockquote 1 1:0.0644343 1 1:0.0309334 1 1:0.0261616 Blockquote"
let regex = try! NSRegularExpression(pattern: "(\\d \\d:[\\.0-9]+)", options: [])
let matches = regex.matchesInString(str, options: [], range: NSMakeRange(0, str.characters.count))
for m in matches.reverse() {
let range = m.rangeAtIndex(1)
let startIndex = str.startIndex.advancedBy(range.location)
let endindex = startIndex.advancedBy(range.length)
let value = str[startIndex..<endindex]
str.replaceRange(startIndex..<endindex, with: "\"\(str[startIndex..<endindex])\"")
}
print(str)

compare 2 arrays and get the values which are not matched :using perl map command or loops simply [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
arr1 = 1,2,3,4,5;
arr2 = 1,2,3;
want to compare and output as arr3=4,5;
Please help
thanks in advance
arry::utils error out, looks like some problem with the package, so that option is ruled out.
sub diff_array {
my ($a1, $a2) = #_;
my %h;
#h{#$a2} = ();
return grep !exists $h{$_}, #$a1;
}
my #arr1 = (1,2,3,4,5);
my #arr2 = (1,2,3);
my #arr3 = diff_array(\#arr1, \#arr2);

In SML, how do I remove the first occurence of a pattern from a list and then return the removed item and the rest of the list? [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 8 years ago.
Improve this question
I'm trying to write an SML function that takes as parameters a list and an item. If the item is present, the function should return a tuple containing the list without the first such occurence of that item and the item just removed. If there are no occurences of the item in the list, the function should return NONE or something similar to indicate this absence.
Try this:
fun same_string(str, lst) =
case lst of
[] => NONE
|x::xs => case same_string(str, xs) of
NONE => if str = x
then SOME(xs)
else NONE
|SOME xs' => SOME (x :: xs')