%% HOMEWORK LESSON 2 % The codes and text of this script are based or extracted from: % % Borgo, M. Soranzo, A. & Grassi, M. (2012). MATLAB for Psychologists. Springer: New York. % Cohen, M.X. (2017). MATLAB for brain and cognitive scientists. The MIT Press: Cambridge. % Hann, B. Valentine, D.T. Essential MATLAB for engineers and scientists. 6th edition. Elsevier: Cambridge. % Rosenbaum, D. A. Vaughan, J. & Wyble, B. (2014). MATLAB for Behavioral Scientists. 2nd edition. Psychology Press: New York. % Wallisch, P. Lusignan, M. Benayoun, M. Baker, T.I. Dickey, A.S. & Hatsopoulos, N. (2014). MATLAB for Neuroscientists: An introduction to scientific computation in MATLAB. 2nd edition. Academic Press: Amsterdan. % Modifications by Rafael N. Ruggiero (rafaruggiero@usp.br) %% Exercises % 1. Initialize a 30 × 4 matrix. Then add a value into the (31,10) position. % Type whos after each step. What happened, and what does this tell you about % how MATLAB deals with matrix sizes and adding new content to existing variables? % 2. One of the important concepts in programming % is to use the output of one command as the input into another command. The code below uses % square brackets to put the ages of the mice into one array called ages, % and then to convert that variable to logical by testing whether the ages are less than 70 days. %Perform the same operation using one line of code. ages = [mouse.age]; ages = ages<70; % 3. Initialize a variable to be a 50 × 40 matrix of zeros. % Then set the first 10 and the last 5 elements in the % first dimension to have the values of 10 and 5, respectively, % for the first 20 elements in the second dimension. % 4.Categorize the elements of the vector x=[-2 3 0 2 -6 1 -2 0 0 -13 12] % as positive, negative, and zero and store them in three separate vectors. % Count the number of elements in each vector. % 5. Which of the following lines of code will produce valid matrices? % What's wrong with the ones that produce an error? % aMatrx = [1 3 2 3; 6 7 8 9]; % aMatrx = [1 3 2 3; 5 3 6; 7 8 9]; % aMatrx = [1 3; 5 6; 7 8]; % aMatrx = [1 3 2; 5 6; 7 8 9]; % 6. For the matrix or matrices in the previous exercise that are valid, % perform the following basic arithmetic operations on them: add the % number 4 to all elements; multiply each element by 6.4; subtract 100. % Then, use indexing to apply these three operations: only to the first % element of the matrices; only to the first row of the matrices; % only to the second column of the matrices. % 8. What is the difference between the functions rand, randn and randi? % rand Uniformly distributed pseudorandom numbers. % randi Pseudorandom integers from a uniform discrete distribution. % randn Normally distributed pseudorandom numbers. % 9. Create the following matrix B = [0.1 0.2 0.3 0.4 0.5 0.6 0.7; 14 12 10 8 6 4 2; 1 1 1 1 0 0 0;... 3 6 9 12 15 18 21]; % a) Create a 3 × 4 matrix B from the 1st, 2nd, and 3rd rows, and the 1st through the 4th columns of the matrix B. % b) Create a 2 × 7 matrix C from the 2nd, and 3rd rows, and all the columns of the matrix B. % c) Do the following command C=B(:); examine the matrix C. Can you explain % what happened? % d) Find the maximum and minimum value of the B matrix using the max and % min functions (help max, help min) % 10. The functions round, fix, ceil and floor are used to round elements % of an array to an integer. Another useful function is rem: rem(x,y) % returns the remainder after x is divided by y. % Read about them in help and use them in the following operations and compare the results: % a) 17/5; b) 13/5; c) 11/5; d) -9/4; % 11. Using the colon operator create the following: % a) a vector from 1 to 100 in steps of 10 % b) a vector from -4 to 0 in steps of 0.2 % c) a matrix which thefirst line goes from 12 to 20 with 5 elements; % second line goes from 3 to -4 with 5 elements; % % Type help linspace and read about this function. Perform the same % operations using linspace % 12. MATLAB has a built-in function named char that creates an array with rows that % have the same number of characters from an input of rows that are not of the same length. % MATLAB makes the length of all the rows equal to the longest row by adding spaces at the end of the short lines. % Read about the function an use with the following text: % Da minha aldeia vejo quanto da terra se pode ver no Universo... % Por isso a minha aldeia é tão grande como outra terra qualquer % Porque eu sou do tamanho do que vejo % E não do tamanho da minha altura... % i. What is the size of this string? % ii. extract the word universe from the string and assign to a variable str1. % iii. extract the large word from the string and assign it to a str2 variable. % iv. concatenate both strings (str1 an str2) using the strcmp function. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 1. Answer the following questions for MATLAB structures: % a) How can structures be created? % b) How can one extract the data from fields of a structure? % c) How can one pre-allocate space for a structure? % 2. Considering the structure you created in class: % name = mickey; % surgeryDate = 24 March; % geneType = wildtype; % age = 85; % in days since birth % numElectrodes = 3; % Now add the informations from another 3 animals: % Rat2, 2 april, DATCre, 77, 6; % Rat3, 2 April, DATCre, 77, 12; % Rat4, 21 March, wildtype, 88, 3; % a) create a vector Nelectrodes that contains numElectrodes information % from all rats; % b) calculate the mean age of the 4 animals; % c) You realized that you made a mistake. Rat3 is actually a wildtype, % born 28 of March, age=81. Fix this mistake on your structure. % d) You know that on average you record 0.6 neurons for each working % electrode. How many neurons do you expect to record in these animals? % 3. Given the code below that creates two workerData structures consisting % of names (string), time to complete items (vector of numbers), and if the items passed % quality control (vector of Booleans). clear; clc; worker1Data.name = 'Bob'; worker1Data.time = [15.7, 12.2, 16.0, 14.8, 19.5, 9.8, 10.1, 20.2]; worker1Data.quality = [true, false, true, true, true, true, false,true]; worker2Data.name = 'Joe'; worker2Data.time = [18.7, 19.9, 23.4, 18.0, 18.7, 20.0]; worker2Data.quality = [false, true, true, false, true, false]; % write code to: % % Display each workerData structure using the MATLAB disp function. How is the % data in structures displayed? % Display each workerData structure using the MATLAB fprintf function. % Extract the time to complete items vector from each structure and store each of them in % a separate row vector. Why can't these be stored in a 2D array with two rows? % Extract the quality control vector from each structure and store each of them in a % separate row vector. % Compute the average time to complete an item for each worker. % Compute the average time to complete an item that passed quality control for each % worker % 4. Answer the following questions for MATLAB cell arrays: % a) How can cell arrays be created? % b) How can one extract the data of a cell array ? % c) How can one preallocate space for a cell array?