MATLAB:
Write a function called matrix_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 = matrix_problem([1 4; 5 2; 3 1], will make B equal to [2 4; 10 2; 6 2]. The function should work for all inputs, not solely for the example provided
matrix_problem1.m
function matrix_problem1(A)
[r,c] = size(A);
B = A(:)';
for i=1:length(B)
if( ~mod(B(i),2)==0)
B(i)=B(i)*2;
end
end
C = reshape(B,[r,c]);
disp('The Matrix with all even numbers is ')
disp(C)
main.m
clear all;
clc;
A=[1 4;5 2;3 1];%example matrix
matrix_problem1(A)
SAMPLE OUTPUT for different input matrices ----------------------->>>>>>>>
>> odd_to_even
1 4
5 2
3 1
The Matrix with all even numbers is
2 4
10 2
6 2
>> odd_to_even
1 4
5 7
3 1
The Matrix with all even numbers is
2 4
10 14
6 2
>> odd_to_even
1 4
5 7
3 1
6 9
The Matrix with all even numbers is
2 4
10 14
6 2
6 18
>> odd_to_even
1 4 15
5 7 12
3 1 2
6 9 11
The Matrix with all even numbers is
2 4 30
10 14 12
6 2 2
6 18 22
>> odd_to_even
1 4 15 9
5 7 12 16
3 1 2 18
6 9 11 6
The Matrix with all even numbers is
2 4 30 18
10 14 12 16
6 2 2 18
6 18 22 6
Get Answers For Free
Most questions answered within 1 hours.