how to extract matrix from big matrix in torch/lua - lua

In torch7 / lua
There is a matrix:
[ 1, 2, 3, 4;
5, 6, 7, 8;
9, 10, 11, 12;
13, 14, 15, 16 ]
how to extract this:
[ 6, 7;
10, 11 ]
and how to overwrite it by matrix operation
[ 1, 2, 3, 4;
5, 78, 66, 8;
9, 45, 21, 12;
13, 14, 15, 16 ]
Thanks in advance.

matrix
matrix:sub(2,3,2,3)
z = torch.Tensor({{78,66},{45,21}})
matrix:sub(2,3,2,3):copy(z)

Related

Decode JSON with multiple keys using Codable

I have stored in iCloud several JSON files as a Byte type. Hope that's correct so far.
I've got to fetch those CKRecords and then parse them and show a graph using the values stored in the JSON. I am capable of fetching the data from iCloud but I got no luck parsing this JSON.
{"00:00": 17, "00:10": 16, "00:20": 17, "00:30": 16, "00:40": 16, "00:50": 17, "01:00": 16, "01:10": 16, "01:20": 16, "01:30": 16, "01:40": 17, "01:50": 17, "02:00": 18, "02:10": 18, "02:20": 17, "02:30": 17, "02:40": 17, "02:50": 17, "03:00": 16, "03:10": 17, "03:20": 16, "03:30": 16, "03:40": 17, "03:50": 16, "04:00": 16, "04:10": 16, "04:20": 16, "04:30": 16, "04:40": 16, "04:50": 18, "05:00": 17, "05:10": 16, "05:20": 17, "05:30": 17, "05:40": 17, "05:50": 17, "06:00": 17, "06:10": 17, "06:20": 16, "06:30": 16, "06:40": 17, "06:50": 15, "07:00": 15, "07:10": 15, "07:20": 14, "07:30": 15, "07:40": 13, "07:50": 11, "08:00": 8, "08:10": 8, "08:20": 7, "08:30": 5, "08:40": 4, "08:50": 4, "09:00": 2, "09:10": 2, "09:20": 9, "09:30": 8, "09:40": 7, "09:50": 7, "10:00": 5, "10:10": 4, "10:20": 6, "10:30": 5, "10:40": 4, "10:50": 4, "11:00": 3, "11:10": 2, "11:20": 2, "11:30": 1, "11:40": 2, "11:50": 1, "12:00": 1, "12:10": 2, "12:20": 3, "12:30": 3, "12:40": 2, "12:50": 3, "13:00": 1, "13:10": 0, "13:20": 1, "13:30": 0, "13:40": 3, "13:50": 2, "14:00": 3, "14:10": 4, "14:20": 3, "14:30": 6, "14:40": 4, "14:50": 6, "15:00": 5, "15:10": 7, "15:20": 7, "15:30": 7, "15:40": 7, "15:50": 6, "16:00": 7, "16:10": 8, "16:20": 8, "16:30": 6, "16:40": 5, "16:50": 5, "17:00": 5, "17:10": 4, "17:20": 4, "17:30": 6, "17:40": 6, "17:50": 5, "18:00": 6, "18:10": 7, "18:20": 4, "18:30": 3, "18:40": 3, "18:50": 5, "19:00": 5, "19:10": 3, "19:20": 4, "19:30": 4, "19:40": 2, "19:50": 4, "20:00": 1, "20:10": 2, "20:20": 2, "20:30": 1, "20:40": 3, "20:50": 2, "21:00": 4, "21:10": 4, "21:20": 7, "21:30": 7, "21:40": 6, "21:50": 7, "22:00": 8, "22:10": 7, "22:20": 8, "22:30": 9, "22:40": 10, "22:50": 10, "23:00": 10, "23:10": 9, "23:20": 9, "23:30": 10, "23:40": 10, "23:50": 10}
I previously used Codable to parse a JSON using Swift but this case is different. I got a lot of keys that are numbers and I don't really know how to approach this.
you can try
let res = try? JSONDecoder().decode([String:Int].self,from:data)

Spliting tables into 9's

I would like to know how can I split my table into subtables of 9's.
Example:
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
Code shall return:
{ {1, 2, 3, 4, 5, 6, 7, 8, 9}, {10, 11, 12, 13, 14, 15, 16, 17, 18}, { 19, 20} }
How do you think is this done?
Your code seems over complicated. The task is to create a subtable every 9 elements. The code below does that:
a={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
b={}
j=0
k=9
for i=1,#a do
if k==9 then j=j+1; b[j]={}; k=0 end
k=k+1
b[j][k]=a[i]
end
Here, j tracks the number of subtables created and k tracks the number of elements added to a subtable. When k becomes 9, a new subtable is created. k starts at 9 to signal that.

How do i append another list within list comprehension

I am trying to find out unique elements and avoid duplicates , between the lists (using list comprehension)
`a = [10 , 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,9]`
`b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]`
dup_list = []
This works , but with duplicates
`final_list = [uniq for uniq in a if a not in dup_list if uniq in b ]`
when i try to append the my dup_list in the last line of the comprehension statement it says invalid syntax
i.e this doesn't work.
"final_list = [uniq for uniq in a if a not in dup_list if uniq in b dup_list.append(uniq) "
I am a newbie in python so apologize for any missed out basic facts .
a = [10, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 9]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
nodupes = sorted([item for item in a if item not in b] +
[item for item in b if item not in a])
That yields [4, 6, 7, 11, 12, 21, 34, 55, 89]
Which is the same answer I get using sets:
seta = set(a)
setb = set(b)
nodupes = sorted(list(seta ^ setb))

Remove multiple ranges of values from array

a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
a2 = [2..4, 8..11, 16..17]
Removing one range of values from an array can be done like this:
[1, 2, 3, 4, 5, 6, 7, 8, 9].slice!(2..5)
Iterating over the ranges and apply the same as above (a2.each { |range| a1.slice!(range) }) isn't perfect though. The ranges overlap sometimes and thus destroy the referencing index for the other ranges.
So, any suggestions on how to remove the ranges in a2 from a1 in the most efficient way?
a1 is normally [*0..10080] long. a2 has about 30 ranges, each containing hundreds of values.
If the result of the first operation impacts the second you're either going to have to track the resulting offset implications, which can get crazy complicated, or simply go about doing the reverse operation and instead flag which you want or don't want using the ranges:
require 'set'
a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
a2 = [2..4, 8..11, 16..17]
# Convert the ranges to a set of index values to remove
reject = Set.new(a2.flat_map(&:to_a))
# Using value/index pairs, accumulate those values which are
# not being excluded by their index.
a1.each_with_index.each_with_object([ ]) do |(v, i), a|
a << v unless (reject.include?(i))
end
# => [0, 1, 5, 6, 7, 12, 13, 14, 15, 18, 19, 20]
[-1, *a2.flat_map(&:minmax), a1.length].each_slice(2).flat_map{|i,j| a1[i+1...j]}
# => [0, 1, 5, 6, 7, 12, 13, 14, 15, 18, 19, 20]
I'm not sure this is the least naive solution, but it seems simple to convert your ranges into arrays so you're dealing with like-for-like:
a2.each{ |a| a1 = a1 - a.to_a }
a1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
a2 = [2..4, 8..11, 16..17]
a1 - a2.flat_map(&:to_a)

Grails sorting asList() , sort()

I want to sort the result here my code:
def games = Game.withCriteria() {
eq('season', currentSeason)
eq('competition', competition)
round {
ge('roundNr', startRound.roundNr)
}
or {
round {
le('roundNr', currentRound.roundNr)
}
gt('state', Game.STATE_NOT_STARTED)
}
order 'date', 'asc'
}
def sortGames = games.asList().sort(games.round.sorting)
The query works fine, i want to sort them.
Error:
Error 500: Executing action [recalcCompetitionTable] of controller [at.ligaportal.CompetitionController] caused exception: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.sort() is applicable for argument types: (java.util.ArrayList) values: [[1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 2, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16]] Possible solutions: sort(), sort(java.util.Comparator), sort(groovy.lang.Closure), wait(), size(), size()
Servlet: grails
URI: /ligaportal/grails/competition/recalcCompetitionTable.dispatch
Exception Message: No signature of method: java.util.ArrayList.sort() is applicable for argument types: (java.util.ArrayList) values: [[1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 2, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16]] Possible solutions: sort(), sort(java.util.Comparator), sort(groovy.lang.Closure), wait(), size(), size()
Caused by: No signature of method: java.util.ArrayList.sort() is applicable for argument types: (java.util.ArrayList) values: [[1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 2, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16]] Possible solutions: sort(), sort(java.util.Comparator), sort(groovy.lang.Closure), wait(), size(), size()
Class: ApiAuthenticationFilter
At Line: [77]
where is my Problem?
I got the Games via withCriteria() method, sort by date.
And all games have rounds , and now i want to sort them by the sorting of the rounds.
5 Games have 1 Round and alle Games have dates.
Round 1 = 18.09.2012 - 25.09.2012 (5 Games)
Round 2 = 26.09.2012 - 31.09.2012 (4 Games) and one Game plays later
5.11.2012
Round 3 = 1.10.2012 - 7.10.2013
And my problem is, i sum up the round with a each and previous round.
Roudn 1 = 3,3,3,1,1 Points
Round 2 = 6,4,4,2,1 Points -> 1 Games later
Round 3 = 9,7,7,4,3 Pounts
Round 2 = 6,4,4,4,1 (after game)
Round 4 = here i cant go back to round 4 -> the one game is lost!
And now i want to order the games via game.round.sorting
This is my problem.
Thank you!
The Solution
def bySorting = new Comparator() {
int compare(a,b) { a.round.sorting <=> b.round.sorting }
}
games.sort(bySorting)
You don't need to call sort()on games due to the result of your criteria is already sorted.
Instead sorting by date : order 'date', 'asc' you could add your desired property here.
If you really need to sort the result you have to pass an Comparator or a Closure to sort():
def sortGames = games.asList().sort{it.round.sorting}
You should provide some more information. Looks like sorting is an ArrayList - how should the games be sorted?
You should be able to sort by round as part of the initial database query by using createAlias in your criteria:
def games = Game.withCriteria() {
createAlias('round', 'rnd')
eq('season', currentSeason)
eq('competition', competition)
ge('rnd.roundNr', startRound.roundNr)
or {
le('rnd.roundNr', currentRound.roundNr)
gt('state', Game.STATE_NOT_STARTED)
}
order 'rnd.sorting', 'asc'
}
This way you don't need to do any sorting of the returned list in Groovy.
However, if you do want to sort an existing list of games by their round.sorting value then you almost had the right syntax. What you actually need is:
def sortGames = games.sort { it.round.sorting }
The Groovy sort method expects a closure that returns the sort key, whereas your attempt was passing a list of all the sort keys for all the games.
From your code it is not clear what games.round.sorting does.
But have a look at the following links to learn more about sorting lists in groovy:
Custom list sorting
Groovy OrderBy
Or google for Groovy Comparator to find other examples and descriptions.

Resources