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...
*** 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...
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...
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
Write a function custom sort(v) that takes as input a vector v and as output returns...
Write a function custom sort(v) that takes as input a vector v and as output returns the vector w sorted into increasing order. For example, if the input is [−2 1 3 1 5], the output should be [−2 1 1 3 5]. Don’t use the built-in ”sort” function or anything similar. matlab question
Write a function count_div5(nested_list) that takes in a list of lists of integers, and returns a...
Write a function count_div5(nested_list) that takes in a list of lists of integers, and returns a list of integers representing how many integers in each sublist of the original were divisible by 5. Examples: >>> count_div5([[5, 3, 25, 4], [46, 7], [5, 10, 15]]) [2, 0, 3] >>> count_div5([]) [] >>> count_div5([[-20, 10, 2, 4, 5], [], [5], [8, 25, 10], [6]]) [3, 0, 1, 2, 0]
using dr.racket programing language If we write a function that tests whether a list contains only...
using dr.racket programing language If we write a function that tests whether a list contains only strings, odd numbers, or even numbers, you will notice that the code that iterates through the list stays the same, with the only change being the predicate function that checks for the desired list element. If we were to write a new function for each of the tests listed above, it would be more error-prone and an example of bad abstraction. We could write...