Skip to content

Instantly share code, notes, and snippets.

@pabloem
Created April 23, 2015 12:44
Show Gist options
  • Save pabloem/62b8841ce4f1c424bf25 to your computer and use it in GitHub Desktop.
Save pabloem/62b8841ce4f1c424bf25 to your computer and use it in GitHub Desktop.
ML homework 3
%###################################################################
% Homework #3. Number of features in a 24x24 pixel image
% Student: Pablo J. Estrada. ID: 2014-25245
%###################################################################
% Here are our patterns. We need only specify their dimensions.
patt_x = [2,1,2,3,1]
patt_y = [1,2,2,1,3]
function res = count(x, y, patt_len,patt_x,patt_y)
res = 0;
if x == 0 || y == 0
res = 0;
else
for i=1:x
for j = 1:y
for k = 1:patt_len
if mod(i,patt_x(k)) == 0 && mod(j,patt_y(k)) == 0
res = res + 1;
end
end
end
end
end
end
function r = total_count(x,y,patt_len,patt_x,patt_y)
r = 0;
if x == 0 || y == 0
r = 0;
else
for i=0:x-1
r = r + count(x-i,y,patt_len,patt_x,patt_y);
end
r = r + total_count(x,y-1,patt_len,patt_x,patt_y);
end
end
res = total_count(24,24,4,patt_x,patt_y);
disp("Total features with abcd patterns: ")
res
res = total_count(24,24,5,patt_x,patt_y);
disp("Total features with abcde patterns: ")
res
%#############
% RUN RESULTS
%#############
% >>>
% Total of features with 'abcd' patterns: 134,736
% Total of features with 'abcde' patterns: 162,336
%#############
####################################################################
# Homework #3. Number of features in a 24x24 pixel image
# Student: Pablo J. Estrada. ID: 2014-25245
####################################################################
# Here are our patterns. We need only specify their dimensions.
allpatterns = {'a':[2,1], 'b':[1,2], 'c':[2,2],'d':[3,1],'e':[1,3]}
# We get only our 'abcd' patterns. Exclude pattern 'e'.
abcdpatterns = {key : allpatterns[key]
for key in allpatterns
if key in {'a','b','c','d'}}
# This function returns the count of possible patterns with their
# upper left corner grounded on the origin.
def count(x,y,patterns):
if x == 0 or y == 0:
return 0
elems = ( 1
if (i+1) % patterns[k][0] == 0 and (j+1) % patterns[k][1] == 0
else 0
for i in range(x) for j in range(y) for k in patterns)
return sum(elems)
# This function uses the function 'count' to scan for
# and count all the possible patterns through the whole image
def total_count(x,y,patterns):
if x == 0 or y == 0:
return 0
sum = 0
# We count the whole upper row; and add the patterns found
for i in range(x):
sum += count(x-i,y,patterns)
# And then we go down to the next row, start over; and add
# the patterns found
sum += total_count(x,y-1,patterns);
return sum
print("Total of features with 'abcd' patterns: " +
"{:,}".format(total_count(24,24,abcdpatterns)))
print("Total of features with 'abcde' patterns: " +
"{:,}".format(total_count(24,24,allpatterns)))
##############
# RUN RESULTS
##############
# >>>
# Total of features with 'abcd' patterns: 134,736
# Total of features with 'abcde' patterns: 162,336
##############
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment