Question

In: Electrical Engineering

Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given).

Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given). Important: Next, the expression "a single line of code" implies a single command or equality. In other words, the code:

X=A+1; X=X+B; is considered to be TWO lines of code, even though it can be written as one line.

(a) (3%) Write a single line of code which saves the first two rows of array A as a new array X.

(b) (4%) Write a single line of code which replaces rows 4 and 5 of array A with rows 2 and 6 of array B.

(c) (3%) Write a single line of code which divides all elements in array A by the corresponding elements in array B and saves the resulted array as X.

(d) (4%) Write a single line of code which adds the transpose of array A to array B and saves the result in array X. What should the number of columns of A and B be so that this operation does not result in an error?

(e) (4%) Write a single line of code which uses the MATLAB function "sum()" to find the sum of each row

(f) (3%) Lets call N the unknown number of columns of arrays A and B. What is the size of array X = [A B]; ?

(g) (3%) Again, N is the number of columns of arrays A and B. What is the size of array X = [A;B]; ?

(h) (3%) Assume that the arrays A and B have more than 5 columns. Write a single line of code that divides all elements in A by the B element located at row 3 and column 4.

(i) (5%) Write a single line of code using the MATLAB function "find()" which finds the location of all elements in A which are greater than 1. Then, write another line of code which replaces all these elements in A by the number 2.

Solutions

Expert Solution

(i)

A = magic(10);
B = A';
X = A(1:2,:);

output:

>> A
A =

    92    99     1     8    15    67    74    51    58    40
    98    80     7    14    16    73    55    57    64    41
     4    81    88    20    22    54    56    63    70    47
    85    87    19    21     3    60    62    69    71    28
    86    93    25     2     9    61    68    75    52    34
    17    24    76    83    90    42    49    26    33    65
    23     5    82    89    91    48    30    32    39    66
    79     6    13    95    97    29    31    38    45    72
    10    12    94    96    78    35    37    44    46    53
    11    18   100    77    84    36    43    50    27    59

>> B
B =

    92    98     4    85    86    17    23    79    10    11
    99    80    81    87    93    24     5     6    12    18
     1     7    88    19    25    76    82    13    94   100
     8    14    20    21     2    83    89    95    96    77
    15    16    22     3     9    90    91    97    78    84
    67    73    54    60    61    42    48    29    35    36
    74    55    56    62    68    49    30    31    37    43
    51    57    63    69    75    26    32    38    44    50
    58    64    70    71    52    33    39    45    46    27
    40    41    47    28    34    65    66    72    53    59

>> X
X =

   92   99    1    8   15   67   74   51   58   40
   98   80    7   14   16   73   55   57   64   41

(ii)

A([4 5],:) = B([2 6],:);

after modification A is

A =

    92    99     1     8    15    67    74    51    58    40
    98    80     7    14    16    73    55    57    64    41
     4    81    88    20    22    54    56    63    70    47
    99    80    81    87    93    24     5     6    12    18
    67    73    54    60    61    42    48    29    35    36
    17    24    76    83    90    42    49    26    33    65
    23     5    82    89    91    48    30    32    39    66
    79     6    13    95    97    29    31    38    45    72
    10    12    94    96    78    35    37    44    46    53
    11    18   100    77    84    36    43    50    27    59

(iii)

X = A./B;

resultant X is

    1.000000    1.010204    0.250000    0.094118    0.174419    3.941176
    0.989899    1.000000    0.086420    0.160920    0.172043    3.041667
    4.000000   11.571429    1.000000    1.052632    0.880000    0.710526
   10.625000    6.214286    0.950000    1.000000    1.500000    0.722892
    5.733333    5.812500    1.136364    0.666667    1.000000    0.677778
    0.253731    0.328767    1.407407    1.383333    1.475410    1.000000
    0.310811    0.090909    1.464286    1.435484    1.338235    0.979592
    1.549020    0.105263    0.206349    1.376812    1.293333    1.115385
    0.172414    0.187500    1.342857    1.352113    1.500000    1.060606
    0.275000    0.439024    2.127660    2.750000    2.470588    0.553846

 Columns 7 through 10:

    3.217391    0.645570    5.800000    3.636364
   11.000000    9.500000    5.333333    2.277778
    0.682927    4.846154    0.744681    0.470000
    0.696629    0.726316    0.739583    0.363636
    0.747253    0.773196    0.666667    0.404762
    1.020833    0.896552    0.942857    1.805556
    1.000000    1.032258    1.054054    1.534884
    0.968750    1.000000    1.022727    1.440000
    0.948718    0.977778    1.000000    1.962963
    0.651515    0.694444    0.509434    1.000000

(iv)

X = A'+B; %A' denotes transpose

X =

   184   196     8   170   172    34    46   158    20    22
   198   160   162   174   186    48    10    12    24    36
     2    14   176    38    50   152   164    26   188   200
    16    28    40    42     4   166   178   190   192   154
    30    32    44     6    18   180   182   194   156   168
   134   146   108   120   122    84    96    58    70    72
   148   110   112   124   136    98    60    62    74    86
   102   114   126   138   150    52    64    76    88   100
   116   128   140   142   104    66    78    90    92    54
    80    82    94    56    68   130   132   144   106   118

number of columns should be 10 as it is to be square


Related Solutions

What's the memory address for the following elements arrays given: Base Address: 1000, Dimensions: 10 rows,...
What's the memory address for the following elements arrays given: Base Address: 1000, Dimensions: 10 rows, 5 columns, 4 byte elements. Elements: 0,0; 9,4; 2,2; 4,1; Base Address: 500, Dimensions: 10 rows, 15 columns, 4 byte elements. Elements: 0,0; 9,14; 2,2; 4,11; Base Address: 800, Dimensions: 4 rows, 5 columns, 2 byte elements. Elements: 0,0; 3,4; 2,2; 3,4;
Write a script to display the following patterns on the screen. Number of rows and columns...
Write a script to display the following patterns on the screen. Number of rows and columns are taken from the command arguments; if they are missing, set default to 3 (rows) and 4 (columns). Hint: you will use a nested loop. **** **** **** a) Display the source code in an editor (#4-11) b) Execute your script in the terminal, and display the command and the result (#4-12)
JAVA If a user inputs a specific number of rows, columns and sections for a rectangle...
JAVA If a user inputs a specific number of rows, columns and sections for a rectangle using three symbols, how would you print using for loops and a string? Not using an array Example: If 4 was inputted for rows, 12 was inputted for columns, 6 was inputted for sections, and the following symbols (that are all chosen by the user) were “$” “*” and “%” the following would print: $$**%%$$**%% $$**%%$$**%% $$**%%$$**%% $$**%%$$**%% Has to account for all numbers...
MATLAB Create a matrix named P which has 2000 rows and 100 columns of all zeros....
MATLAB Create a matrix named P which has 2000 rows and 100 columns of all zeros. Replace the zeros in rows 46-58 and columns 3-18 of matrix P from the previous question with ones.
1 How can we specify the number of rows and columns in a grid layout? 2....
1 How can we specify the number of rows and columns in a grid layout? 2. What happens when we don't have enough items to be displayed in a grid layout with a specified size? 3. How can we specify a grid layout component to occupy more than one columns or rows? 4. What happens if the components in a linear and grid layout exceed the layout size? How can we handle this problem so that all components can be...
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result:             25   69   51   26 171...
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result: 25 69 51 26 171 68 35...
Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E.
4.8.2: Nested loops: Print seats. C++Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Ex: numRows = 2 and numColumns = 3 prints:1A 1B 1C 2A 2B 2C#includeusing namespace std;int main() {int numRows;int numColumns;int currentRow;int currentColumn;char currentColumnLetter;cin >> numRows;cin >> numColumns;/* Your solution goes here */cout << endl;return 0;}
The effects of detergent brand name (factor A in columns) and the temperature of the water (factor B in rows) on the brightness of washed fabrics are being studied
The effects of detergent brand name (factor A in columns) and the temperature of the water (factor B in rows) on the brightness of washed fabrics are being studied. Four brand names and two temperature levels are used; six observations for each combination are examined. The following ANOVA table is produced. ANOVA             Source of Variation SS   df MS F p-value Sample 75   1 75 63.38 8.92E−10 Columns 130.25   3 43.42 36.69...
You are given two integer arrays a and b of the same length. Let's define the...
You are given two integer arrays a and b of the same length. Let's define the difference between a and b as the sum of absolute differences of corresponding elements: difference = |a[0] - b[0]| + |a[1] - b[1]| + ... + |a[a.length - 1] - b[b.length - 1]| You can replace one element of a with any other element of a. Your task is to return the minimum possible difference between a and b that can be achieved by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT