I've got the following code:
while rounds<=5
fprintf('Rolling the dice...\n');
roll=randi(6,1,5);
roll=sort(roll);
fprintf('You rolled:');
disp(roll);
rollCount=rollCount+1;
for x=1:2:17
y=all(ismember(roll,rule{x}))
disp(ismember(roll,rule{x}));
z=all(ismember(rule{x},roll))
disp(ismember(rule{x},roll));
rounds=rounds+1;
end
end
What it SHOULD do is compare the roll array to the rule{x} array and tell me if it's a match. If it's not a match, it should tell me which indexes of the roll array aren't matching the rule array. It's not working correctly however. Say the example roll is [2 2 3 5 5] and the rule{x} is [1 2 3 4 5].
The output I'd like is an array that has [0 1 1 0 1] but the ones I get out of y is [1 1 1 1 1] and for z is [0 1 1 0 1]. That might seem like the right output, but if we change the rule to [5 5 5 5 5] I get [1 1 1 1 1] which is incorrect.
This is for a Yahtzee game I'm writing. The roll is the roll of the dice, and the rule is what I'm trying to match against so I can see what ones I need to re-roll to try and get it to match.
EDIT:
Using the code from dspyz, I wrote the function:
function[scoreCode]=ForwardChaining(rollFunc,ruleFunc)
temp=histc(rollFunc,1:6);
for x=1:2:11
if (ruleFunc{x}<=temp)
scoreCode=ruleFunc{x+1};
break;
else scoreCode=0;
end
end
The main function calls this as:
c= ForwardChaining(roll,rule);
if c == 12;
break;
end
But for some reason, even after 100,000 iterations it doesn't stop, which I take as it not working as intended.
Since the dice can only take on values from 1 to 6. Why not instead generate the histogram counts of each roll.
ie take your (row-)vector of rolls v and say
a = histc(roll, 1:6);
Now if you want to check if a rule is a subset of a (where the rule itself is also phrased in terms of counts of each number), you can just check if
rule <= a
The roll satisfies the rule if this is true in all 6 indices
To clarify:
I don't know about a first-grader, but given a set of (possibly-repeated) values for example [1, 1, 2, 4, 5] where everything is from 1 to 6, we can represent this by counting how many of each number from 1 to 6 is present. In this example:
1: 2
2: 1
3: 0
4: 1
5: 1
6: 0
Now we want to compare this against a rule of the same form but with possibly fewer elements. For example [1, 2, 3, 4]. The counts for this rule would be
1: 1
2: 1
3: 1
4: 1
5: 0
6: 0
To check if [1, 2, 3, 4] is a subset of [1, 1, 2, 4, 5], we only need to know if the counts for [1, 2, 3, 4] are all less than or equal to the counts of [1, 1, 2, 4, 5]. Ie we want to check if
1: 1 <= 2
2: 1 <= 1
3: 1 <= 0
4: 1 <= 1
5: 0 <= 1
6: 0 <= 0
All of these are true except for 3 so we know that [1 2 3 4] is not a subset of [1 1 2 4 6] because it contains no 3
But if all 6 inequalities are true, then it would be
ex. If you want to know if [1 3 3 3 5] contains [3 3 3], you can check all of
1: 0 <= 1
2: 0 <= 0
3: 3 <= 3
4: 0 <= 0
5: 0 <= 1
6: 0 <= 0
which is true
EDIT: Looking at MATLAB's documentation, it says the argument to histc must be sorted
EDIT 2: Oops, got that wrong, it says the second argument must be sorted. Changing it back.
I am not sure what your arrays are doing exactly, but the idea is you do if you have [2 2 3 5 5] and the rule{x} is [1 2 3 4 5], simply doing:
[2 2 3 5 5] == [1 2 3 4 5] will get you [0 1 1 0 1] or something like A=(roll==rule)
Related
I have some code in golang which is suppose to discover the next possibilities in a Tic-Tac-Toe board.
This is the buggy part:
var next []State
for row := 0; row < len(board); row++ {
for place := 0; place < len(board[row]); place++ {
if board[row][place] == 0 {
nPos := board
fmt.Print(nPos)
nPos[row][place] = play
fmt.Print(nPos, row, place, play, "\n")
next = append(next, nPos)
}
}
}
State is a type of [][]int.
board is a State, play is an int and next is a []State .
The output is as follows:
[[0 0 0] [0 0 0] [0 0 0]][[1 0 0] [1 0 0] [1 0 0]] 0 0 1
[[1 0 0] [1 0 0] [1 0 0]][[1 1 0] [1 1 0] [1 1 0]] 0 1 1
[[1 1 0] [1 1 0] [1 1 0]][[1 1 1] [1 1 1] [1 1 1]] 0 2 1
[[[1 1 1] [1 1 1] [1 1 1]] [[1 1 1] [1 1 1] [1 1 1]] [[1 1 1] [1 1 1] [1 1 1]]]
You can clearly see two things:
One iteration changes the whole column (I guess it has to do with the outer loop, row)
For some reason the changes are saved (nPos is not reinitialized through iterations)
I am somewhat new to Go, am I wrong when expect nPos to be a new variable in every iteration?
I have already looked for issues in the line nPos[row][place] = play, but apparently no specific line causes the issue. I guess it is just the scope.
As #zerkms pointed out:
nPos := board <--- here both nPos, and board contain the same slice, Go does not implicitly do a deep slice copy. If you want a duplicate of a slice - you should manually clone it.
One option is:
cpy := make([]T, len(orig)) copy(cpy, orig)
Other answers are here:
Concisely deep copy a slice?
I have wrote a function named array_slice which gets four numbers n, n_dim, n_row, n_col from the user and performs array operations given below.
Instructions:
Create an array x of shape (n_dim, n_row, n_col), having first n natural numbers.
Create a Boolean array b of shape (2,).
Print the values for following expressions: x[b] and x[b,:,1:3]
For example if we have input 30, 2, 3, 5, for each corresponding parameters n, n_dim, n_row, n_col, Then the output prints will be as:
[[[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]]]
[[[ 1 2] [ 6 7] [11 12]]]
The written code is:
import numpy as np
# Enter your code here. Read input from STDIN. Print output to STDOUT
def array_slice(n,n_dim,n_row,n_col):
x=np.array(n, dtype=int, ndmin=n_dim).reshape(n_row,n_col)
b=np.array([True,False],dtype="bool",ndmin=n_dim).reshape(2,)
print(x[b])
print(x[b,:,1:3])
if __name__ == '__main__':
n = int(input())
n_dim = int(input())
n_row = int(input())
n_col = int(input())
array_slice(n,n_dim,n_row,n_col)
I went through official documentation NumPy, but still couldn't understand the error. I tried all possible ways with arange and array but I'm unable to get solution. Please help me out
This passed all test cases:
x = np.arange(n, dtype=int).reshape(n_dim, n_row, n_col)
b = np.array([True, False], dtype="bool", ndmin=n_dim).reshape(2,)
print(x[b])
print(x[b, :, 1:3])
I have tried the following code for x array using np.arrange:
x = np.arange(n, dtype=int).reshape(n_dim, n_row, n_col)
it will work:
[[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]]
[[[ 1 2]
[ 6 7]
[11 12]]]
Lets say I have the following array:
board = np.random.randint(1, 9, size=(2, 5))
How do I remove duplicates from each element in the array
e.g.
[[6 1 2 8 4]
[8 3 2 3 6]]
So here there are two 3s, and I want one of those to be deleted, how can I perform such an action?
Given your example, it seems that you don't want repetition relatively to rows. You may be interested in numpy.random.choice and try something like this:
import numpy as np
nb_lines = 2
nb_columns = 5
min_value = 1
max_value = 9
range_value = max_value-min_value
# The number of columns should be <= than the integer range to have a solution
assert(range_value+1 >= nb_columns)
board = min_value + np.array([
np.random.choice(np.arange(range_value+1), nb_columns, replace=False)
for l in range(nb_lines)
])
print(board)
Output:
% python3 script.py
[[7 4 6 3 1]
[2 8 6 4 3]]
I dont understand the result the block produce in the following examples :
The following array initialization :
my_arr = Array.new(10) { |e| e = e * 2 }
produce the following result :
[0 2 4 6 8 10 12 14 16 18]
Though I would expect a : [0 0 0 0 0 0 0 0 0 0].
In my opinion, the element e is not initialzed, so at best its value could be zero (though I would expect a 'nil'), and zero times some number always return zero. Or I would expect it to be a geometric progression, not an arithmetic progression. So why ???
So there must be something I miss.
And the following code :
my_arr = Array.new(10) { |e| e = e + 2 }
produce the following result :
[2, 3, 4, 5, 6, 7, 8 , 9, 10, 11]
Though I would expect a : [0 2 4 6 8 10 12 14 16 18].
In my opinion, the element e is not initialzed, so at best its value could be zero (though I would expect a 'nil'). So it should be an arythmetic progression by 2, not by 1. Why ???
I checked the ruby reference and can't find any clue to understand these so unintuitive results.
The idea come from here http://www.tutorialspoint.com/ruby/ruby_arrays.htm
I think the point relies in explaining what value is the e supposed to be representing ?
This can be written as:
Array.new(10) { |index| index * 2 } #=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Which matches the final form in the documentation to which you linked:
new(size) {|index| block }
Where an array of the given size (10) is created. Each element in this array is created by passing the element’s index to the given block and storing the return value.
The value you assign to e in your example is discarded after each element is initialised.
I have a cell array that looks like this:
Column1 Column2
[1 2 3 4] [2 5 6 9]
[1 3 4] [3 4 7 8]
[2 3 4] [1 3 7 9]
[1 2 4] [1 4 6 8]
There are a few more columns that have similar styles of data. I need to create a way to make a graph of each column (separate graphs for each column of the array), that plots each point as a number from each double as the x-coordinate, and the row as the y-coordinate. It should look something like this:
(Row)
1 x x x x
2 x x x
3 x x x
4 x x x
1 2 3 4
X is just a point on the graph.
Does this make enough sense? I feel like I'm making 0 progress in explaining what I want. If anyone doesn't understand this, feel free to ask questions and I'll answer them as best I can.
Something like this?
cin = { {[1 2 3 4] , [1 3 4], [2 3 4], [1 2 4]}, {[1 2 3 8] , [1 3 4], [2 3 4], [1 2 4]} };
for k=1:numel(cin)
col_k = cin{k};
figure(); %// 1 figure per column
for y=1:numel(col_k)
plot(col_k{y}, y);
hold on;
end
end