You are on page 1of 5

NATIONAL UNIVERSITY

551 M.F. JHOCSON ST., SAMPALOC, MANILA

COLLEGE OF ENGINEERING

Jerald M. Habil
Name/Section Rating

Date performed 09/27/18


Date submitted 10/11/18

EXPERIMENT NO. 1-2


VARIABLES, EXPRESSIONS AND ARRAYS

Question 1: What are the syntaxes available for save and load commands? List them and write brief
information
Answer:
save(FILENAME) - stores all variables from the current workspace in a MATLAB formatted binary file
(MAT-file) called FILENAME. Specify FILENAME as a character vector or a string scalar

save(FILENAME,VARIABLES) - stores only the specified variables. Specify FILENAME and


VARIABLES as character vectors or string scalars.

save(FILENAME,'-struct',STRUCTNAME,FIELDNAMES) - stores the fields of the specified scalar


structure as individual variables in the file. If you include the optional FIELDNAMES, the save function
stores only the specified fields of the structure.

save(FILENAME, ..., '-append') - adds new variables to an existing file.

save(FILENAME, ..., FORMAT) - saves in the specified format: '-mat' or '-ascii'.

save(FILENAME, ..., VERSION) - saves to MAT-files in the specified version: '-v4', '-v6', '-v7', or '-v7.3'.

save(FILENAME, ..., '-nocompression', '-v7.3') - saves to MAT-files version 7.3 without compression.

save FILENAME ... - Is the command form of the syntax, for convenient saving from the command line.
With command syntax, you do not need to enclose FILENAME in single or double quotation marks.
Separate inputs with spaces instead of commas.

S = load(FILENAME) - loads the variables from a MAT-file into a structure array, or data from an ASCII
file into a double-precision array.

S = load(FILENAME, VARIABLES) - loads only the specified variables from a MAT-file. Specify
FILENAME and VARIABLES as character vectors or string scalars.

S = load(FILENAME, '-ascii') - forces load to treat the file as an ASCII file, regardless of the extension.

load(...)-loads without combining MAT - file variables into a structurearray.

load ... - is the command form of the syntax, for convenient loading from the command line. With
command syntax, you do not need to enclose inputs in single or double quotation marks. Separate
inputs with spaces instead of commas. Do not use command syntax if FILENAME is a variable.

Page | 1
Question 2: Use MATLAB Help to find information about the command format. Summarize the
information below.
Answer: format does not affect how MATLAB computations are done. Computations on float variables,
namely single or double, are done in appropriate floating point precision, no matter how those variables
are displayed. Computations on integer variables are done natively in integer. Integer variables are
always displayed to the appropriate number of digits for the class, for example, 3 digits to display the
INT8 range -128:127. Format SHORT and LONG do not affect the display of integer variables.

Question 3: Perform the following operations and write the syntax you have used and the answers
provided by MATLAB. You may or may not assign each expression into a variable. Check your answers
with manual calculations or with a calculator.
Answer:
EXPRESSIONS SYNTAX ANSWER
1 2 [1/(2 + 3 ^ 2)] + [1/(2 ^ 2 - 3)] 1.0909
2
+ 2
2+3 2 −3
3(42 + 52 + 62 ) [3*(4^2 + 5^2 + 6^2)] / [2*(1 + 8.2500
2
2(1 + 2 + 3 ) 3 2^2 + 3^2)]
1 4 2 3 2 [(1 + 1 /3) ^ 4] – [(2 /3 - 3 /2) ^ 2 14.4660
(1 + ) − ( − ) + 12 + 12]
3 3 2
1 +2+3
( 1 + 22 + 34 ) 10 x = (1 + 2 + 3) /10; 14.8780
(1^2 + 2^3 + 3^4)^x
(1 − 2)2 − (22 − 1) x = (1 - 2)^2; -0.0020
2
(1 + 2 + 3 )3 2 y = (2^2 - 1);
z = (1 + 2^2 + 3^3)^2;
(x - y)/z

Question 4: Produce the plot of x against t and save the figure as Figure.jpg.
Answer:

Page | 2
Question 5: Briefly explain the last three commands that you have issued.
Answer:
>> fs = 8e3; %assigning fs as a value of 8*e*3.
>> t = 0:(1/fs):0.05 %assigning t as the value from the range of 0 to 0.05
>> x = sin(2*pi*440*t) %assigning x as a value of sin(2*pi*440*t)

Question 6: Generate the matrix below in at least two ways. Write the syntaxes you have used in each
case.
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
[5 6 7 8 9 ]

Answer: 1st Syntax - x = [1 2 3 4 5;2 3 4 5 6; 3 4 5 6 7;4 5 6 7 8; 5 6 7 8 9]


2nd Syntax - x = [1,2,3,4,5;2,3,4,5,6;3,4,5,6,7;4,5,6,7,8;5,6,7,8,9]

Question 7: List down all the elementary matrices and matrix manipulation functions.
Answer:

Elementary Matrices:
zeros - Zeros array.
ones - Ones array.
eye - Identity matrix.
repmat - Replicate and tile array.
repelem - Replicate elements of an array.
linspace - Linearly spaced vector.
logspace - Logarithmically spaced vector.
freqspace - Frequency spacing for frequency response.
meshgrid - X and Y arrays for 3-D plots.
accumarray - Construct an array with accumulation.
: - Regularly spaced vector and index into matrix.

Matrix Manipulation Functions


cat -Concatenate arrays.
reshape -Reshape array.
diag -Diagonal matrices and diagonals of matrix.
blkdiag -Block diagonal concatenation.
tril -Extract lower triangular part.
triu -Extract upper triangular part.
fliplr -Flip matrix in left/right direction.
flipud -Flip matrix in up/down direction.
flip -Flip the order of elements.
rot90 -Rotate matrix 90 degrees.
: -Regularly spaced vector and index into matrix.
find -Find indices of nonzero elements.
end -Last index.
sub2ind -Linear index from multiple subscripts.
ind2sub -Multiple subscripts from linear index.
bsxfun -Binary singleton expansion function.

Page | 3
Question 8: What is the difference between vertical and horizontal concatenation?
Answer: When you concatenate two matrices by separating those using commas, they are just
appended horizontally. It is called horizontal concatenation. Alternatively, if you concatenate two
matrices by separating those using semicolons, they are appended vertically. It is called vertical
concatenation.

Question 9: Summarize the conversion that happens when numbers of different data classes are
operated on each other.
Answer: When the operation that you will be converting is on its default operation the output will be the
same, but if you convert the default operation to another operation i.e, double precision to single
precision the output will be displayed as yo execute its command.

Question 10: Show how other words can be extracted from the MarkA variable.
Answer: As the comman is shown below you can extract other words by, example you want to extract
the word countrymen, first is count the previous characters (including the spacing) up to the first letter
of the word you want to extract. From “F” of Friends up to “C” of countrymen It has a 17 character then
separate it by colon : then enter the last number of countrymen up to its last word

>> MarkA = 'Friends, Romans, countrymen, lend me your ears.'


>> Nationality = MarkA(1,18:27) %The extracted word will be countrymen

Question 11: What is the difference between a character array and a string?.
Answer: The Array is separated by a comma and the characters is enclosed by a apostrophe ‘ ‘. The
other one the String is separated by spacing and the characters is enclosed by quotation marks “ “.

Question 12: Recreate the cell array as shown in figure 5 and the structure array in figure 6. Write the
MATLAB commands below.

Page | 4
Answer: Type your answer here. Use normal fonts for your responses. For commands, scripts and
functions use Courier New as font type. Place your commands, scripts and functions inside a box:
such as shown.
For Cell Array
>>myCell = cell(2,3)
>> myCell{1,1} = [ 3 4 2; 9 7 8; 8 5 1]
>> myCell{1,2} = {['Anne Smith']; ['9/12/94']; ['Class II']; ['Obs.1'];[
'Obs.2']}
>> myCell{1,3} = [.25 + 3*i 8 - 16*i; 34 + 5*i 7 + 92*i]
>> myCell{2,1} = {[1.43 2.98 7.83 5.67 ];[4.21]}
>> myCell{2,2} = [-7 2 -14; 8 3 -45; 52 -16 3]
>> myCell{2,3} = {['text'], [4 2; 1 5]; [7.3 2.5; 1.4 0], [.02 + 8*i]}

For Structure Array


>> aquarium(1).name = 'Mushu';
>>aquarium(1).fundamentalfrequency = '175';
>>aquarium(1).amplitude = '2';
>>aquarium(1).decayrate = '1.5';
>>aquarium(1).modulationfrequency = '0.65';
>>aquarium(1).numberofharmonics = '3';
>> >> aquarium(2).name = 'Willy';
>> aquarium(2).fundamentalfrequency = '180';
>> aquarium(2).amplitude = '2';
>> aquarium(2).decayrate = '1.5';
>> aquarium(2).modulationfrequency = '0.65';
>> aquarium(2).numberofharmonics = '3';
>> aquarium(3).name = 'Shamu';
>> aquarium(3).fundamentalfrequency = '165';
>> aquarium(3).amplitude = '1'
>> aquarium(3).amplitude = '1';
>> aquarium(3).decayrate = '0.8';
>> aquarium(3).modulationfrequency = '1.0';
>> aquarium(3).numberofharmonics = '4';

Conclusions and Recommendations: Therefore, I conclude that you can perform many mathematical
computations on MATLAB by using a different types of commands. MATLAB is like a calculator but it
can perform much better than the ordinary scientific calculators, such as matrix manipulation, creating
any types of vectors and plot it and creating a various of Data Types such as Cell Array and Structure
Array.

Page | 5

You might also like