0% found this document useful (0 votes)
14 views

Lec 5

Matlap

Uploaded by

alamyrh847
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lec 5

Matlap

Uploaded by

alamyrh847
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

**Array and Matrix Operations

1 2 −1 3 0 5
𝑎=[ ] , 𝑏=[ ] 𝑡ℎ𝑒𝑛 𝑎 + 𝑏 = [ ]
3 4 −2 1 1 5

Note that for matrix multiplication to work, the number of columns in


matrix a must be equal to the number of rows in matrix b.
**Common Array and Matrix Operations
**Special Values

MATLAB includes a number of predefined special values. These


predefined values may be used at any time in MATLAB without
initializing them first. A list of the most common predefined values is
given in Table

**Displaying Output Data


There are several ways to display output data in MATLAB. The
simplest way is one we have already seen—just leave the semicolon off
the end of a statement, and it will be echoed to the Command Window.
We will now explore a few other ways to display data.
**Changing the Default Format
When data is echoed in the Command Window, integer values are
always displayed as integers, character values are displayed as strings,
and other values are printed using a default format. The default format
for MATLAB shows four digits after the decimal point, and it may be
displayed in scientific notation with an exponent if the number is too
large or too small. For example, the statements

You can change the format by selecting the “Preferences” icon on the
Toolstrip. This option will pop up the Preferences Window, and the
format can be selected from the “Variables” item in the preferences list
see Figure
**The disp Function
Another way to display data is with the disp function. The disp function
accepts an array argument and displays the value of the array in the
Command Window. If the array is of type char, then the character string
contained in the array is printed out.
This function is often combined with the functions num2str (convert a
number to a string) and int2str (convert an integer to a string) to create
messages to be displayed in the Command Window. For example, the
following MATLAB statements will display
“The value of pi = 3.1416” in the Command Window. The first
statement creates a string array containing the message, and the second
statement displays the message.
str = ['The value of pi = ' num2str(pi)];
disp (str);

**Formatted Output with the fprintf Function


An even more flexible way to display data is with the fprintf function.
The (fprintf) function displays one or more values together with related
text, and lets the programmer control the way that the displayed value
appears.
The general form of this function when it is used to print to the
Command Window is:
fprintf(format,data)
where format is a string describing the way the data is to be printed and
data is one or more scalars or arrays to be printed. The format is a
character string containing text to be printed plus special characters
describing the format of the data. For example, the function
fprintf('The value of pi is %f \n',pi)
will print out 'The value of pi is 3.141593'
The characters %f are called conversion characters; they indicate that a
value in the data list should be printed out in floating-point format at
that location in the format string. The characters \n are escape
characters; they indicate that a line feed should be issued so that the
following text starts on a new line. There are many types of conversion
characters and escape characters that may be used in an fprintf function.
A few of them are listed in Table.
It is also possible to specify the width of the field in which a number
will be displayed and the number of decimal places to display. This is
done by specifying the width and precision after the % sign and before
the f. For example, the function
fprintf('The value of pi is %6.2f \n',pi)
will print out 'The value of pi is 3.14'
The conversion characters %6.2f indicate that the first data item in the
function should be printed out in floating-point format in a field six
characters wide, including two digits after the decimal point.

You might also like