Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) How do you compare variables in Matlab? Can you compare a scalar to a vector?

ID: 3879996 • Letter: 1

Question

1)How do you compare variables in Matlab? Can you compare a scalar to a vector? A vector to a vector of same dimensions? A vector to a vector of different dimensions? A scalar to a matrix? A vector to a matrix? If the

answer is yes to any of these questions, describe what the result represents.

3) In Matlab, what is the difference between a “function” and a “script”? How are both of these different from the standpoint of what you type in and execute in the Command Window? Give an example of a function with parameters in an M-Xile and a script M-Xile and show how to execute each from the command line.

4) Understand the difference in how Matlab represents a row vector, a column vector, and a matrix. How do you Xind the dimensions of a variable in Matlab? How do you select a single element or a subset of elements from an array or matrix in Matlab? Give examples of code that does each of these.

2) How do you compare variables in Matlab? Can you compare a scalar to a vector? A vector to a vector of same dimensions? A vector to a vector of different dimensions? A scalar to a matrix? A vector to a matrix? If the answer is yes to any of these questions, describe what the result represents 3) In Matlab, what is the difference between a "function" and a "script"? How are both of these different from the standpoint of what you type in and execute in the Command Window? Give an example of a function with parameters in an M-file and a script M-file and show how to execute each from the command line. 4) Understand the difference in how Matlab represents a row vector, a column vector, and a matrix. How do you find the dimensions of a variable in Matlab? How do you select a single element or a subset of elements from an array or matrix in Matlab? Give examples of code that does each of these.

Explanation / Answer

If you post more than 1 question, as per chegg guidelines I have to solve only 1 question.

4.

In matlab,

a row vector is a vector which has only 1 row and one or more than 1 columns. A row vector is represented as

[1 2 3 4]

This is a row vector of size 4.

a column vector is a vector which has only 1 columnand one or more than 1 rows. A column vector is represented as

[1

2

3

4]

This a a column vector of 4 rows.

A matrix is a 2D array with more one or more than 1 no of both rows and columns. A matrix is represented as

[ 1 2 3

4 5 6

7 8 9 ]

To get the dimensions of a variable, use the function size() which returns the length and width of the vector.

To select a single element of a vector, use

x = [ 1 2 3 4 ]

This is the vector. To access an element at given index i

x(i)

To select a given subset of elements, use

x( start_index : end_index )

Sample Code:

% create a row vector

x = [10 : 20];

% display elements at index 2

disp(x(2));

% display a range of elements

disp(x(4 : 8));

% create a column vector

y = [10 ; 11 ; 12 ; 13 ; 14 ; 15];

% display elements at index 3

disp(y(3));

% create a 2 D matrix

z = [ 1 : 3 ; 4 : 6 ; 7 : 9 ];

% display elements at index (2, 3)

disp(z(2, 3));

Sample Output:

11

13 14 15 16 17

12

6