HW3 - 3101 Matlab -- (INSERT NAME and UNI HERE)

Due 11:59pm on Tuesday, April 1st, 2008

Contents

Goals

Working with new data structures: strings, and cells, avoiding loops, saving and loading variables to files

Directions

Please fill in your name at the top of this page and write your answers under their corresponding question. When you are done, publish this file to html using File > Publish To HTML (or the command publish('hw1.m', 'html') and create a new zip file that is labeled with your UNI and homework number, in this format: bs2018_hw1.zip, that contains this original m file, and the html directory created from publishing the file. Make sure the html file adequately shows your work, (has images, etc.), and then email this file to cs3101@gmail.com, making sure to include your name, and uni in the subject.

Extra credit

There will be extra credit points assigned on problems that are marked with (EC for no loop), if you are able to answer the question without using a loop.

Problem 1 - Working with Strings

teststring1 = 'The MATLAB high-performance language for technical computing integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation.';

1.1

Convert teststring1 to lowercase and store that new string in a variable called s1 and display s1.

1.2

Using one command (no loops!), find the indices of all the characters in s1 that are letters (not punctuation or spaces), then display only the first 5 of those indices.

1.3

Convert s1 into an array of number values (stored as doubles) that correspond to the ASCII values for each character. Call this array x1 and display only the first 5 elements of it.

1.4

Compute and plot a histogram of the letter distribution in x1. Make sure your histogram has 26 bins, and that you are only counting lowercase characters in x1, not any punctuation. (EC for no loop)

1.4.1

Compute and display a new string called s1count which contains the 26 lowercase characters a,b,c...z arranged in order by their number of occurences in s1. The first character of s1count should be the character most used in teststring1. (EC for no loop)

1.5

At what index index in teststring1 does the word 'environment' appear?

Problem 2 - Working with Cells

2.1

Make a new cell called e1. Fill e1 with the inidividual words from teststring1, so that each element of e1 is a word from teststrin1. The words should be lowercase and stripped of any punctuation (except you should make sure to leave hyphens).

2.2

Show the two different ways ({}, ()), to index the 6th element of e1, explain with a brief comment what is returned by each technique, and why they are different

2.3

Set N to the number of words in teststring1 and display N

2.4

Create a new cell called c1 that is NxN, each element of c1 should correspond to comparing two words that are in teststring1. in each element of c1, store 3 sub-elements: the first word, the second word, and the number of characters that the two words have in common.

2.4.1

display the 1st element of c1

2.5

Using c1 that you computed in the previous problem, how many letters do the 9th and 10 words have in common? (make sure your answer is a number and not a cell, and furthermore the command to get this value should be only one line, no semicolons)

Problem 3 - Saving and loading variables

3.1

Print out the names of the variables you have declared so far in the this homework and make sure the ouput shows the sizes and classes of those variables.

3.2

How many variables have you declared? (Answer this question with a command, not by couting yourself).

3.3

Save all of your current variables to a file called hw3temp.mat.

3.4

Clear your workspace and also clear your command window.

3.5

How many variables do you have in your workspace now?

3.6

Load all of the variables in hw3temp.mat back into the workspace.

3.7

How many variables do you have in your workspace now?

Problem 4 - ROT13 encryption

Implementing rot13 encryption, for more info Wikipedia: ROT 13

4.1

Write a short code snippet that takes as input a string called s and creates a new string called s13 that is rot13 encoded. initially set s to the contents of teststring1, and display s13 when the loop is done. Make sure that you properly preserve uppercase and lowercase letters as well as punctuation. (EC for no loop)

4.2

Show how applying the rot13 algorithm twice, gives back the original string. copy and paste your rot13 code again below, but this time set s equal to the s13 that you just computed. display the new s13, (which should be indentical to the original teststring1.

4.3

Write a short snippet of code that determines if s13 and teststring1 are identical, and if they are then display the words, 'The two strings are the same, and both have x characters', where x is properly filled in with the length of the string. If the strings are not the same, display the words 'The two strings are different'.

Problem 5 - Breaking the Caesar Cipher

http://en.wikipedia.org/wiki/Caesar_cipher

5.0

Print out the working current directory.

5.1

Load in the file hw3caesar.mat, which can be downloaded from the class website.

5.2

Display only the variables that are contained within hw3caesar.mat

5.2.1

Display the variable encryptedtext

5.3

Write a short program to decode the string stored in the encryptedtext variable in hw3caesar.mat. The encrypted text has been encoded with a caesar cipher with an unknown shift value. Your task is to to find that shift value without explicitly shifting the encrypted text 26 times. First compute the histogram of the encrypted text and then simply compare shifted versions of that histogram with the histogram of traintext. The correct shift value is the one for which the two histograms have the lowest chi square statistic. It is helpful to first convert the traintext and encrypted text to lower case before you create and compare the histograms. when you are done, print out the hidden shift value you found.

5.4

Now that you have found the shift value from the previous problem, what does the encrypted text say?

5.5

Declare a new variable called translatedtext and set it equal to what you have decoded from the encrypted text. save ONLY the tranlated text variable to a file called hw3tt.mat, after that print out the variables that are containted within the file hw3tt.mat.