Question: MATLAB: Write a function called problem1 that takes a matrix A of positive integers as its sole input. If the assumption is wrong, the function

MATLAB:

Write a function called problem1 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 = hw3_problem1([1 4; 5 2; 3 1], will make B equal to [2 4; 10 2; 6 2]. PLEASE HELP I HAVE POSTED THIS MANY TIMES AND HAVE GOTTEN INCORRECT SOLUTIONS EVERY TIME. IT SHOULD WORK EVEN FOR INPUTS OF 1,2 (ETC.)

The code I have right now is:

function [retMat] = hw3_problem1(A) [m,n] = size(A); flag = 1; for i = 1:m for j = 1:n if A(i,j) < 0 flag = 0; end end end if flag == 0 retMat = []; else retMat = A; count = 1; for i = 1:m for j = 1:n if count % 2 == 1 retMat(i,j) = 2* A(i,j); end count = count + 1; end end end end

This works for input of 1 but fails for input of 2, Please help write a new code or by fixing this one. Much appreciated.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!