Question

1. Write a MATLAB function in a separate file and name it mat_function, this function should...

1. Write a MATLAB function in a separate file and name it mat_function, this function should accept the matrix mat_in as its first input and the real number num_in as the second input, and return the following three outputs: I. Replace each element in mat_in that is greater than num_in by 1 and each element that is less than num_in by zero to get the new matrix mat1_out. II. Replace the diagonal of mat1_out by the diagonal of mat_in while keeping all the other elements of mat1_out to get the new matrix mat3_out.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

clc%clears screen
clear all%clears history
close all%closes all files
[mat1_out,mat3_out]=mat_function(randi([1,5],3,3),3)


function [mat1_out,mat3_out]=mat_function(mat_in,num_in)
mat1_out=[];
for i=1:size(mat_in,1)
for j=1:size(mat_in,2)
if(mat_in(i,j)>num_in)
mat_out(i,j)=1;
elseif(mat_in(i,j)<num_in)
mat_out(i,j)=0;
else
mat1_out(i,j)=mat_in(i,j);
end
end
end
mat3_out=mat1_out;
for i=1:size(mat1_out,1)
for j=1:size(mat1_out,2)
if(i==j)
mat3_out(i,j)=mat_in(i,j);
end
end
end
end

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
1. Write a MATLAB function in a separate file and name it mat_function, this function should...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name...

    matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name the function [max_value, max_index] - LASTNAMES matrix max (x), where input argument x is a vector or matrix (not a scalar), and the outputs are the maximum value(s) and indexes of the maximum value(s) of the mput x. Do not use MATLAB functions max() or find(). Use loops and if branches to determine the maximum elements of matrix. Check for correct number and valid...

  • answer 8 using matlab Using nested loops, traverse through the matrix for the following. a) Replace...

    answer 8 using matlab Using nested loops, traverse through the matrix for the following. a) Replace all elements divisible by 3 with 100. b) Replace all elements divisible by 2 with 50. c) Replace the first element of each row with 10. d) Display the all elements of the 2D matrix as a 10 array (9 elements in 1 row) 8) Write a function file that determines the maximum element of an input matrix A using nested loops and counts...

  • MATLAB QUESTION Write a MATLAB function called vector_input that takes no inputs and returns no outputs. Instead, when c...

    MATLAB QUESTION Write a MATLAB function called vector_input that takes no inputs and returns no outputs. Instead, when called, it gives control to the user and asks the user to input a length-3 vector. Then the function prints: The sum of ___, ____, and ____ is ____. [new line] where the first three blanks are filled by each element in input vector, and the last blank is the sum of all three elements.

  • This basic problem demonstrates the while-loop. (USE MATLAB) Write a Matlab function called steps that takes one scalar...

    This basic problem demonstrates the while-loop. (USE MATLAB) Write a Matlab function called steps that takes one scalar as an input argument and returns two scalars as output arguments. If it is called this way [n, d_left] = steps(d), then it sets n equal to the minimum number of steps required to move from a distance of one foot away from a wall to a distance less than d feet from the wall. The first step is 1/2 foot. If...

  • Matlab 1 question help Question5 Given X [ 3 562; 189 6; 4071], write MATLAB command...

    Matlab 1 question help Question5 Given X [ 3 562; 189 6; 4071], write MATLAB command to do the following operations: a) Multiply the value of the elements in first and third rows and second and fourth columns by 3 and store it in another matrix, what will be its value b) Calculates the number of the elements in each column greater than 5 c) Get the min number in the second column

  • Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as t...

    Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as the input a provided string for the sample name (sname) and number for strength (strgth) - open a text file named mytensiledata.txt, with permission to read or append the file. - Make sure to capture any error in opening the file - Write sname and strgth to the file - Close the file...

  • Write your answer code into one function file on matlab

     Write your answer code into one function file on matlab The spiral of Archimedes. The spiral of Archimedes is a curve described in polar coordinates by the equation r= kθ Where r is the distance of a point from the origin, and d is the angle of that point in radians with respect to the origin. Write a function file to compute the spiral of Archimedes with varying number of inputs (no input, one input, two inputs) for k, 0 and varying number of...

  • Exercise 1: Write MATLAB code to create a 5x5 matrix A with 2's on the diagonal, and -1 on the su...

    Exercise 1: Write MATLAB code to create a 5x5 matrix A with 2's on the diagonal, and -1 on the super- and sub-diagonal. Then replace the (1,1) element of A with a 1. (Make your commands capable of handling an arbitary sized NxN matrix by first defining N=5, then using the variable N as the size in each of the commands.)

  • Part I - Functions Write a program to do the following: • . Create two separate...

    Part I - Functions Write a program to do the following: • . Create two separate lists: ["Female", "M", 1, 4, "Mail", 0, "F"] o [-23,1,35,52,23,0,1000] For each element in the first list, replace each element using the following rules: o Change the value to Female if the element is Female, F, or 1 o Change the value to Male if the element is Male, M, or 0 o Otherwise, change it to "Undefined" For each number in the second...

  • This code NEEDS TO BE DONE IN MATLAB!!!! Write a function that takes one input, an...

    This code NEEDS TO BE DONE IN MATLAB!!!! Write a function that takes one input, an integer number n, and returns a matrix that is nxn, where the value of each number is the distance from the upper-left to bottom-right diagonal. (Use while loops if you can!) Numbers below the diagonal should be negative, and numbers above the diagonal should be positive. For example, if your input was 5, your output would be: 0 1 2 3 4 -1 0...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT