Question

In: Electrical Engineering

Convert the following pieces of code in just a single line of code. In all cases, A and B are arrays of size 5 x 5.

Convert the following pieces of code in just a single line of code. In all cases, A and B are arrays of size 5 x 5. Important: The expression "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.

(a) (4%) for i=1:5,

for j=1:5

A(i,j)=B(i,j)+1;

end

end

_________________________________

(b) (4%) for i=1:3

for j=2:4

A(i,j)=B(i,j);

end

end

_________________________

(c) (5%) for i=1:3

for j=2:4

A(i,j)=B(i+2,j+1);

end

end

_________________________________

(d) (4%) for i=1:5

for j=1:5

A(i,j)=1/(B(i,j))^2;

end

end

_________________________________

(e) (5%) for i=1:5

for j=1:5

A(i,j)=B(6-i,j);

end

end

_________________________________

Solutions

Expert Solution

Consider B

   17   24    1    8   15
   23    5    7   14   16
    4    6   13   20   22
   10   12   19   21    3
   11   18   25    2    9

for i=1:5
for j=1:5
A(i,j)=B(i,j)+1;
end
end

Above code can be replaced by single line using

A = B+1; %each element of A is populated from corresponding element of B by adding 1

>> A
A =

   18   25    2    9   16
   24    6    8   15   17
    5    7   14   21   23
   11   13   20   22    4
   12   19   26    3   10

>> B
B =

   17   24    1    8   15
   23    5    7   14   16
    4    6   13   20   22
   10   12   19   21    3
   11   18   25    2    9

>> B+1
ans =

   18   25    2    9   16
   24    6    8   15   17
    5    7   14   21   23
   11   13   20   22    4
   12   19   26    3   10

for i=1:3

for j=2:4

A(i,j)=B(i,j);

end

end

Can be replaced by

A(1:3,2:4) = B(1:3,2:4)

for i=1:3

for j=2:4

A(i,j)=B(i+2,j+1);

end

end

can be replaced by A(1:3,2:4)=B(3:5,3:5);

part d

for i=1:5

for j=1:5

A(i,j)=1/(B(i,j))^2;

end

end

Can be replaced by A = 1./(B.^2)

for i=1:5

for j=1:5

A(i,j)=B(6-i,j);

end

end

can be replaced by

A(1:5,1:5) = B(5:1,5:1);


Related Solutions

What are the values in arrays a, b, and c after the following code executes (list...
What are the values in arrays a, b, and c after the following code executes (list all of the elements of the arrays)? double[] a = new double[4]; double[] b = {6,4,2}; a[a.length-1] = b[b.length-1]; double[] c = b; c[0] = -1; b[1] = c[2]; c = a; c[0] = -2; a[1] = c[3];
Using this sample matlab code: clear all; clc A= ????????; B= ????????; AUG=[A B]; for L=1:size(A,2)...
Using this sample matlab code: clear all; clc A= ????????; B= ????????; AUG=[A B]; for L=1:size(A,2) %Pivoting starts for k=L:size(AUG,1) for m=k+1:size(AUG,1) if abs(AUG(k,L))<abs(AUG(m,L)) temp=AUG(m,:); AUG(m,:)=?????????; AUG(k,:)=?????????; end end end %Pivoting ends %Gauss Elimination starts for k=L+1:size(AUG,1) AUG(k,:)= ????????????????????????????; AA=AUG(:,1:size(A,2)) BB=AUG(:,size(A,2)+1:end) end %Gauss Elimination ends end b)Write a MATLAB M-file which performs gauss elimination without pivoting step by step and shows the coefficient matrix in each step. Using cond (X, P) calculate the condition number of the final step coefficient...
1. Write Verilog code and test bench for Moore FSM having a single input line ‘X’...
1. Write Verilog code and test bench for Moore FSM having a single input line ‘X’ and a single output-line ’Z’. An output of 1 is to be produced coincident with the pattern 1101 and an output of ‘ 0’ is to be produced for all the other sequences and simulate it.
A) Convert 1101.11011101 x 223 to IEEE Standard 754 for single precision floating-point binary format. B)...
A) Convert 1101.11011101 x 223 to IEEE Standard 754 for single precision floating-point binary format. B) Convert the IEEE Standard 754 number 11001010100011010101000000000000 to its decimal equivalent.  
What is the output of the following code: x = 0 a = 1 b =...
What is the output of the following code: x = 0 a = 1 b = -3 if a > 0:    if b < 0:       x = x + 5    elif a > 5:       x = x + 4    else:       x = x + 3 else:     x = x + 2 print(x) 4 2 5 3 Consider the following code segment:    sum = 0.0    while True:       number = input(“Enter a...
Convert the following decimal number into (a) binary and (b) Octal (SHOW ALL STEPS) 205.75
Convert the following decimal number into (a) binary and (b) Octal (SHOW ALL STEPS) 205.75
Find the EAR in each of the following cases: a. 8% compounded quarterly b. 5% compounded...
Find the EAR in each of the following cases: a. 8% compounded quarterly b. 5% compounded monthly c. 9% compounded daily d. 15% with continuous compounding
C++ Code Using all for loops 1. Print 5 lines with 1 asterisk per line 2....
C++ Code Using all for loops 1. Print 5 lines with 1 asterisk per line 2. Print 5 asterisk on 1 line. Endl at the end of the line. 3. Ask for a positive # and print that many asterik on the next line. 4. Using 2 for loops one inside of another that only prints 1 asterik, print a hill shaped triangle: Ex( Input a number a: 4 * ** *** **** 5. Change the for statements to print...
If x is a binomial random variable, compute P(x) for each of the following cases: (a)  P(x≤5),n=9,p=0.7P(x≤5),n=9,p=0.7...
If x is a binomial random variable, compute P(x) for each of the following cases: (a)  P(x≤5),n=9,p=0.7P(x≤5),n=9,p=0.7 (b)  P(x>1),n=9,p=0.1P(x>1),n=9,p=0.1 (c)  P(x<3),n=5,p=0.6P(x<3),n=5,p=0.6 (d)  P(x≥1),n=6,p=0.9P(x≥1),n=6,p=0.9
Write pseudo code for the following parts of class BinarySearchTree: a) find(x) b) contains(x) c) add(x)...
Write pseudo code for the following parts of class BinarySearchTree: a) find(x) b) contains(x) c) add(x) d) remove(x) e) splice(x) f) min() g) max() h) pred() i) succ() j) floor() k) ceil()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT