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

MATLAB: For this problem, you are prohibited from using explicit loops (i.e., fo

ID: 2080971 • Letter: M

Question

MATLAB:

For this problem, you are prohibited from using explicit loops (i.e., for-loop or while-loop). You must use logical indexing instead. Note that an if statement is not a loop and can be used in your solution.

Write a function called problem4 that takes a matrix A of positive integers as its sole input. If the assumption is wrong, the function returns an empty matrix. Otherwise, the function doubles every odd element of A and returns the resulting matrix. Notice that the output matrix will have all even elements. For example, the call B = problem4([1 4; 5 2; 3 1], will make B equal to [2 4; 10 2; 6 2].

Explanation / Answer

Ans)

Matlab Code

===========

function B = problem4(A)
%PROBLEM4 Summary of this function goes here
% Detailed explanation goes here
if A>0

k=find(rem(A,2)~=0 & A~=2); %find the indexes of odd numbers
A(k)=A(k)*2;%double the every odd element
B=A; %copy new matrix to B

else
  
B=zeros(size(A)); %if given matrix has negative integers then return empty matrix
end

end

========================

Result in command window

=======

>> problem4([1 4; 5 2; 3 1])

ans =

2 4
10 2
6 2

>> problem4([1 4; -5 -2; 3 1])

ans =

0

>>

>> problem4(0)

ans =

0

>> problem4([0 0;0 0])

ans =

0 0
0 0

>>