dynamic-programming

Vectorizing sums of different diagonals in a matrix

孤人 提交于 2019-12-01 15:52:28
I want to vectorize the following MATLAB code. I think it must be simple but I'm finding it confusing nevertheless. r = some constant less than m or n [m,n] = size(C); S = zeros(m-r,n-r); for i=1:m-r+1 for j=1:n-r+1 S(i,j) = sum(diag(C(i:i+r-1,j:j+r-1))); end end The code calculates a table of scores, S , for a dynamic programming algorithm, from another score table, C . The diagonal summing is to generate scores for individual pieces of the data used to generate C , for all possible pieces (of size r). Thanks in advance for any answers! Sorry if this one should be obvious... Note The built-in

A way to dynamically create variables in Matlab?

六眼飞鱼酱① 提交于 2019-12-01 13:00:37
The case I am working on is dividing a big three-dimensional array of data that I have collected using good coding practises (etc...) and now I need to segment the layers of this array into separate variables for individual processing elsewhere, I can't call my data like this BigData(:,:,n) . So I would like to create a loop where I create new variables like so for i=1:n createVariable('user_' i) = BigData(:,:,i); end How do I do this without writing n new variables by hand every time? user_1 = BigData(:,:,1); user_2 = BigData(:,:,2); user_3 = BigData(:,:,3); . . . Your disclaimer sounds

Finding maximum valued subset in which PartitionProblem algorithm returns true

ⅰ亾dé卋堺 提交于 2019-12-01 12:16:37
I´ve got the following assignment. You have a multiset S with of 1<=N<=22 elements. Each element has a positive value of up to 10000000. Assmuming that there are two subsets s1 and s2 of S in which the sum of the values of all the elements of one is equal to the sum of the value of all the elements of the other and it is the highest possible value. I have to return which elements of S would not be included in either of the two subsets. Its probably been solved before, I think its some variant of the Partition problem but I can´t find it. If anyone could point me in the right direction that´d

Longest common prefix length of all substrings and a string

痞子三分冷 提交于 2019-12-01 11:24:33
问题 I found similar questions on StackOverflow, but my question is different. Given a string s contains lowercase alphabet . I want to find the length of Longest common Prefix of all substrings. For example s = 'ababac' Then substrings are as follow: 1: s(1, 6) = ababac 2: s(2, 6) = babac 3: s(3, 6) = abac 4: s(4, 6) = bac 5: s(5, 6) = ac 6: s(6, 6) = c Now, The lengths of LCP of all substrings are as follow 1: len(LCP(s(1, 6), s)) = 6 2: len(LCP(s(2, 6), s)) = 0 3: len(LCP(s(3, 6), s)) = 3 4:

Dynamic Programing approach for a subset sum

萝らか妹 提交于 2019-12-01 10:50:34
Given the following Input 10 4 3 5 5 7 Where 10 = Total Score 4 = 4 players 3 = Score by player 1 5 = Score by player 2 5 = Score by player 3 7 = Score by player 4 I am to print players who's combine score adds to total so output can be 1 4 because player 1 + player 4 score = 3 + 7 -> 10 or output can be 2 3 because player 2 + player 3 score = 5 + 5 -> 10 So it is quite similar to a subset sum problem. I am relatively new to dynamic programing but after getting help on stackoverflow and reading dynamic programing tutorials online and watch few videos online for past 3 days. The following code

Finding maximum valued subset in which PartitionProblem algorithm returns true

给你一囗甜甜゛ 提交于 2019-12-01 09:29:04
问题 I´ve got the following assignment. You have a multiset S with of 1<=N<=22 elements. Each element has a positive value of up to 10000000. Assmuming that there are two subsets s1 and s2 of S in which the sum of the values of all the elements of one is equal to the sum of the value of all the elements of the other and it is the highest possible value. I have to return which elements of S would not be included in either of the two subsets. Its probably been solved before, I think its some variant

Number of all combinations in Knapsack task

二次信任 提交于 2019-12-01 09:09:38
There is a classic Knapsack problem . My version of this problem is a little different. Given set of items, each with a mass, determine the number of combinations to pack items so that the total weight is less than or equal to a given limit. For example, there is 5 items with mass: 1, 1, 3, 4, 5 . There is a bug with limit = 7 . There are the following combinations: 1 + 3 1 + 4 1 + 5 1 + 1 + 3 1 + 1 + 4 1 + 1 + 5 3 3 + 4 4 5 Is there a way to count number of combinations without brute? This is one solution: items = [1,1,3,4,5] knapsack = [] limit = 7 def print_solutions(current_item, knapsack,

Arrange n items in k nonempty groups such that the difference between the minimum element and the maximum element of each group is minimized

大憨熊 提交于 2019-12-01 08:48:01
Given N items with values x[1], ..., x[n] and an integer K find a linear time algorithm to arrange these N items in K non empty groups such that in each group the range (difference between minimum and maximum element values/keys in each group) is minimized and therefore the sum of the ranges is minimum. For example given N=4 , K=2 and the elements 1 1 4 3 the minimum range is 1 for groups (1,1) and (4,3) . You can binary search the answer. Assume the optimal answer is x . Now you should verify whether we can group the items into k groups where the maximum difference between the group items is

Arrange n items in k nonempty groups such that the difference between the minimum element and the maximum element of each group is minimized

帅比萌擦擦* 提交于 2019-12-01 06:13:35
问题 Given N items with values x[1], ..., x[n] and an integer K find a linear time algorithm to arrange these N items in K non empty groups such that in each group the range (difference between minimum and maximum element values/keys in each group) is minimized and therefore the sum of the ranges is minimum. For example given N=4 , K=2 and the elements 1 1 4 3 the minimum range is 1 for groups (1,1) and (4,3) . 回答1: You can binary search the answer. Assume the optimal answer is x . Now you should

Find maximum length of good path in a grid

情到浓时终转凉″ 提交于 2019-12-01 05:22:35
Given is a N*N grid.Now we need to find a good path of maximum length , where good path is defined as follow : Good path always start from a cell marked as 0 We are only allowed to move Left,Right,Up Or Down If the value of ith cell is say A, then value of next cell in the path must be A+1. Now given these few conditions, I need to find out the length of maximum path that can be made. Also I need to count such paths that are of maximum length. Example : Let N=3 and we have 3*3 matrix as follow : 0 3 2 3 0 1 2 1 0 Then maximum good path length here is 3 and the count of such good paths is 4. 0