Write a program that computes array B by computing the natur

Write a program that computes array B by computing the natural logarithm of all the elements of A whose values are equal to or greater than 1, and adding 20 to each element that is less than 1. Do this by using a for loop with conditional statements. (Hint: Create a B matrix full of 1s or 0s, then determine the value of each element of B. You can use a for loop for each column or each row)

3 5 -4 6 8 25 s2 a»

Solution

Please follow the code and comments for description :

CODE :

A = [ 3, 5, -4; -6, 8, 25; -17, 9, -1];
B = zeros(size(A));
for i=1:size(A,1)
   for j=1:size(A,2)
       if(A(i,j) >= 1)
           B(i,j) = log(A(i,j))
       else
           B(i,j) = A(i,j)+20
       end
   end  
end

OUTPUT :

B =

   1.09861   0.00000   0.00000
   0.00000   0.00000   0.00000
   0.00000   0.00000   0.00000

B =

   1.09861   1.60944   0.00000
   0.00000   0.00000   0.00000
   0.00000   0.00000   0.00000

B =

    1.09861    1.60944   16.00000
    0.00000    0.00000    0.00000
    0.00000    0.00000    0.00000

B =

    1.09861    1.60944   16.00000
   14.00000    0.00000    0.00000
    0.00000    0.00000    0.00000

B =

    1.09861    1.60944   16.00000
   14.00000    2.07944    0.00000
    0.00000    0.00000    0.00000

B =

    1.09861    1.60944   16.00000
   14.00000    2.07944    3.21888
    0.00000    0.00000    0.00000

B =

    1.09861    1.60944   16.00000
   14.00000    2.07944    3.21888
    3.00000    0.00000    0.00000

B =

    1.09861    1.60944   16.00000
   14.00000    2.07944    3.21888
    3.00000    2.19722    0.00000

B =

    1.0986    1.6094   16.0000
   14.0000    2.0794    3.2189
    3.0000    2.1972   19.0000

  
Hope this is helpful.

Write a program that computes array B by computing the natural logarithm of all the elements of A whose values are equal to or greater than 1, and adding 20 to
Write a program that computes array B by computing the natural logarithm of all the elements of A whose values are equal to or greater than 1, and adding 20 to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site