top of page

# MATLAB CHALLENGES FOR BEGINNERS

The following MATLAB challenges are meant to provide concrete examples to help you learn MATLAB programming. Try to go through them as fast as you can. Your ultimate goal is to analyze real data, so let’s not get stuck too long here. The challenges can also be adapted for any other programming language such as Python or Julia (although you would need to do a bit of additional work to figure out the specific syntax for the functions used below).

###### Challenges similar to real data sets

Challenges in this section are created that resemble those we often face with real experimental data. Experiments are made up but exist in similar form in the literature. The corresponding data are simulated.

###### Challenge 1: Speech Intelligibility

Background: 10 participants listened to several sentences, such as ‘The bread was bought in a store’. After each sentence they reported back what they heard. Sentences were masked by added background noise at two difficulty levels: easy and hard. For each of the 10 participants, the proportion of correctly reported sentences was calculated for each difficulty level.

Copy the following two data vectors into the MATLAB command window:

EASY = [0.86 0.89 0.9 0.78 0.95 0.81 0.8 0.91 0.85 0.84]

HARD = [0.8 0.67 0.78 0.82 0.65 0.7 0.74 0.76 0.69 0.79]

Each entry in these two vectors reflects the proportion of correctly heard sentences of one participant.

• Use the ‘mean’ and ‘std’ functions to calculate the average and the standard deviation for each of the two difficulty conditions (results: for EASY: mean = 0.859, std = 0.0538; for HARD: mean = 0.74, std = 0.0593).

• Use the ‘errorbar’ function to plot the average and standard deviation for each of the two conditions (i.e., the plot should contain two error bars each centered on the mean of the respective condition).

• Use the ‘ttest’ function (dependent samples t-test) to investigate whether the two conditions differ significantly (result: p-value = 0.0035).

###### Challenge 2: Story enjoyment

Background: 10 participants listened to a spoken story and indicated on a scale ranging from 0 to 10 how much they enjoyed the story (0 – not at all, 10 – very much). 5 of the 10 participants had heard the story before, the other 5 heard the story for the first time. Copy the following two lines into the MATLAB command window:

​​​GROUP = [1 1 1 1 1 2 2 2 2 2]

​ENJOY = [6 7 6 5 8 10 9 8 9 7]

The vector GROUP contains the group membership (1 – people who heard the story before, 2 – people how heard it for the first time). The vector ENJOY contains the enjoyment ratings for the story for each of the participants. That is, entry one in GROUP corresponds to entry one in ENJOY etc.

• Use the ‘mean’ function to calculate the average rating score for group 2. This will require you to first find the indices (or entry numbers) for each person of group 2 in the GROUP vector. Use the ‘find’ function (or logical indexing) to find the indices in the GROUP vector. Then use the indices with the ENJOY vector to extract only the rating data for group 2. Then calculate the average across those 5 values (result: 8.6).

• Calculate the average rating score for group 1 (result: 6.4).

• Extract the ratings for group 1 and group 2 as you just did, but do not average across participants. Instead, feed the two vectors (each should have a length of 5) into the ‘ranksum’ function (non-parametric test) to examine whether the enjoyment ratings differ between the two groups (result: p = 0.0397).

###### Challenge 3: Reaction time to sounds of different intensity

Background: One participant listened to sounds that differed in sound intensity. Their task was to respond as fast as possible when they hear a sound. The participant listening to sounds at five different levels and each sound level was repeated four times in randomized order. Hence, the participant listened to 20 sounds and pressed a response button 20 times. We are interested in the relationship between sound intensity and response times. Copy the following two lines into the MATLAB command window:

intensity = [30 70 40 60 50 30 50 70 40 60 40 30 50 70 60 70 50 30 40 60];

rt = [0.6 0.21 0.54 0.3 0.4 0.54 0.39 0.3 0.52 0.38 0.45 0.47 0.36 0.36 0.3 0.29 0.37 0.43 0.44 0.32];

The vector ‘intensity’ comprises the sound intensities in decibel (dB) for the 20 trials (sound presentations) and the vector ‘rt’ comprises the corresponding 20 reaction times in seconds.