question archive Develop a MATLAB program to create matrix MATH, considering the following: 11

Develop a MATLAB program to create matrix MATH, considering the following: 11

Subject:StatisticsPrice:9.82 Bought3

Develop a MATLAB program to create matrix MATH, considering the following: 11.5t ROW ELEMENTS:
lstterm— 1; last term — ll; spacing 2 2"" HOW ELEMENTS: 1|.?t term— CI: last term — 25: spacing 5 3"" HOW
ELEMENTS: il.St term— it); last term — ED; number of elements —6

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Our MATA matrix generator function should be able to generate the following:

 

>> myMATAGen

ans =

     1     3     5     7     9    11
     0     5    10    15    20    25
    10     0     0     0     0    60

 

The following code would be able to achieve the result:

%****************************************************************************
%
% Function Name: myMATAGen()
% 
% Parameters: None
%
% Description: Generates a matrix with the following criteria:
%              1st Row Elements: 1st term - 1; last term - 11; spacing 2
%              2nd Row Elements: 1st term - 0; last term - 25; spacing 5
%              3rd Row Elements: 1st term - 10; last term - 60; number of
%              elements - 6.

function MATA = myMATAGen()
% Generate an initial function
MATA = zeros(3,6);   % Represents 3 Rows and 6 elements per row
MATA(1,:) = 1:2:11;
MATA(2,:) = 0:5:25;
MATA(3,1) = 10;
MATA(3,6) = 60;
end

Step-by-step explanation

Below is an explanation of each line of code of the MATA matrix generator program:

 

Line 1

MATA = zeros(3,6);   % Represents 3 Rows and 6 elements per row
  • This is merely a means to generate a matrix of defined size, but with 0 as all elements.
    • 3 - represents the 3 rows needed.
    • 6 - represents 6 elements that would be put per row.
  • Removing this line would still make the matrix generator work but is highly recommended to be included for memory purposes.

Line 2

MATA(1,:) = 1:2:11;
  • This line of code generates the following portion of the matrix:
    • First Row Elements: 1st term - 1; last term - 11; spacing 2
  • It used the MATLAB array generator with the following form:
    • h=j:i:k
    • Where:
      • j−firstelementofthearray
      • i−spacebetweenelementsofthearray
      • k−lastelementofthearray
    • Another way to express this is:
      • h=[j?j+1⋅i?j+2⋅i?...?j+fix(ik−j?)?]
      • Note: fix is rounding to the integer nearest to 0.
    • This form generates an array with elements conforming to the criteria of j, i, and k.
      • if with the given j(first element) and i(space between) you cannot get the exact k (last element), it will choose the value that is closest (not greater) to k.
  • On the left side, MATA(1,:) means we are accessing the first row, all elements.
    • In general:   A(n,:)
      • Means: nth row, all elements of Matrix A.

Line 3

MATA(2,:) = 0:5:25;
  • This line of code is for generating the Second Row.
  • This uses the same principle of Line 2.
    • However, instead of MATA(1,:), we use MATA(2,:) to access the Second Row of the matrix.

 

Line 4

MATA(3,1) = 10;
  • This line of code is for generating the 1st element of the 3rd row.
  • The expression on the left MATA(3,1) means to access the element at the 3rd Row1st Column of the matrix.
    • Which could also be interpreted as the First Element of the Third Row.
  • In this case, it is assigning 10 to that spot in the matrix.

 

Line 5

MATA(3,6) = 60;
  • This line of code is for generating the last element of the 3rd row.
  • This line shares similar principle to Line 4.
  • Notice now that we have MATA(3,6) on the left.
    • This means we are assigning a value at the Third Row, 6th Column of the matrix.
    • We assume that the 6th column is the last because it is the total number of elements in that row.
      • Note: MATLAB follows a 1-start index (i.e. the first element has an index of 1).