RESOURCES
For many years now, we’ve been sharing useful resources and media stories related to our research projects. As a leading Science Laboratory in the San Francisco area, it’s important for us to engage with the community and keep them informed about the incredible work and developments they’re helping to support.
RESOURCES
For many years now, we’ve been sharing useful resources and media stories related to our research projects. As a leading Science Laboratory in the San Francisco area, it’s important for us to engage with the community and keep them informed about the incredible work and developments they’re helping to support.
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.
Your tasks:

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 ttest) to investigate whether the two conditions differ significantly (result: pvalue = 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.
Your tasks:

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 (nonparametric 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.
Your tasks:

First, calculate a vector with the unique sound intensities using the ‘unique’ function. Call the vector ‘uniqInt’. The vector ‘uniqInt’ should contain [30 40 50 60 70].

Then use a ‘for’ loop to loop over each entry in ‘uniqInt’ to extract at each iteration the four trials for a specific sound level. You can use “for i = 1 : 5” or better use the ‘length’ function with “for i = 1 : length(uniqInt)". Inside the for loop, you could for example use “ix = intensity == uniqInt(i)” to obtain a logical indexing vector ‘ix’ whose entries are 1 for the intensity in uniqInt(i), but zero for all other entries.

Still inside the loop, use the logical indexing vector ‘ix’ to obtain the reaction times from the ‘rt’ vector that correspond to the specific sound intensity under investigation in the specific iteration of the for loop. For example, “rt(ix)” gets you the four reaction times corresponding to the four trials during which the specific sound intensity was presented.

Still inside the loop, use the ‘mean’ function to calculate the average reaction time across the four reaction times corresponding to the specific sound intensity. Store the result in a vector called ‘meanRT’. The entries of ‘meanRT’ fill up with each loop iteration. Hence, the loop index ‘i’ is used with ‘meanRT’, in this way: meanRT(i). This is all we do within the for loop.

As a final step, we would want to plot the results. Use the ‘plot’ function with ‘uniqInt’ as your xvalues and ‘meanRT’ as your yvalues. Both vectors should have a length of 5. The plot should show faster reaction times for sounds presented at a higher intensity. The plot should like the one on the right.