Question

MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its...

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function called sum_half that takes as input a square matrix A and computes the...
Write a function called sum_half that takes as input a square matrix A and computes the sum of its elements that are in the upper right triangular part of A, that is, elements in the diagonal and elements that are to the right of it. For example, if the input is [1 2 3; 4 5 6; 7 8 9], then the function would return 26. (That is, 1+2+3+5+6+9) Note, the function triu is not allowed. Please write as you...
IN MATLAB: Write a function file that takes a vector as an input and returns another...
IN MATLAB: Write a function file that takes a vector as an input and returns another vector with all repeated elements of the original vector. For example, the vector [3 4 1 0 4 -5 7 3] would return the vector [3 4]. Do not use the built-in function "repelem."
Using nested loops, write a function called primes(a,b) that takes in two positive integers a and...
Using nested loops, write a function called primes(a,b) that takes in two positive integers a and b (where a<b). Then simply RETURNS a string containing all the prime numbers between a and b (or if there are none, the string "No Primes"). You should check that a and b are valid inputs, that is, that a and b are integers such that a<b (otherwise, the function should print “No Primes”). Three sample calls of your function (in IDLE) should produce...
Programming Languages Write an ML function call ch. This function takes a tuple of length 4...
Programming Languages Write an ML function call ch. This function takes a tuple of length 4 and returns “yes” if the second element is greater than the first; otherwise, it returns "no". The function need not behave properly if there are less than or more than 4 elements; $ ch (4,5,6,7); no $ ch (7,9,8,4); yes
In the code space provided in Matlab Grader, create a function that: 1. Accepts an input...
In the code space provided in Matlab Grader, create a function that: 1. Accepts an input matrix A as its only input argument. Matrix A is a 3x3 matrix of random integers between 1 and 4 2. Produces and output matrix B as its only output argument. Matrix B will also be a 3x3 matrix. 3. Calculates the elements of matrix B such that: a. the values of B along the diagonal are equal to the respective elements in A...
*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except...
*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except for 1s on the reverse diagonal from top right to bottom left. The reverse diagonal of an n-by-n matrix consists of the elements at the following indexes: (1, n), (2, n-1), (3, n-2), … (n, 1). The function takes one positive integer input argument named n, which is the size of the matrix, and returns the matrix itself as an output argument. Note that...
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the...
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the elements of an input list in pairs. That is the first element of the resulting list is the sum of the first two elements of the input, the second element of the resulting list is the sum of the 3rd and 4th elements of the input, and so on. If there is an odd number of elements, then the last element remains unchanged. As...
Python Implement function allEven() that takes a list of integers and returns True if all integers...
Python Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10]) False
C Programming: Write a function that takes in an array of integers and an integer containing...
C Programming: Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT