Question

4) Write a function leadzero(v) which takes as input a vector v and as output, c, returns the number of zeros at the beginnin
Matlab code
0 0
Add a comment Improve this question Transcribed image text
Answer #1

4) Code:-

function c = leadzero(v)
c=0; %Intialize c to zero
for x=v %Iterate through the vector
if x==0 % check if the element is zero
c=c+1; %Increment c
else
break; % Break the loop if condition fails   
end
end
end
  

leadzero.m E function c = leadzero (v) c=0; %Intialize c to zero Iterate through the vector for x=v * check if the element is

Command Window Input the vector >> prompt = v=input (prompt); count= leadzero (v) Input the vector [-5,5,11] count = >> Inp
  

5) Code:-

function c = countnon(A)
c=0; %Intialize c to zero
for idx = 1:numel(A) %iterate through the matrix
if A(idx) ~=0 %check if element is not zero
c=c+1; %increment c
end   
end
end
  

countnon.m function c - countnon (A) SIntialize c to zero c=0; for idx = 1:nume1 (A) 3 šiterate through the matrix if A(idx)

  Command Window Input the matrix; >> prompt = A=input (prompt); c= countnon (A) Input the matrix [1,0,3;0,0,5] >> prompt =

Please upvote if you found this solution useful and comment for any doubts.

Add a comment
Know the answer?
Add Answer to:
Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as...
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
  • 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...

  • Matlab: Write a function called minimax that takes M, a matrix input argument and returns mmr,...

    Matlab: Write a function called minimax that takes M, a matrix input argument and returns mmr, a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. As a second output argument called mmm, it provides the difference between the maximum and minimum element in the entire matrix. See the code below for an example: >> A = randi(100,3,4) A = 66 94 75 18 4 68 40 71 85 76...

  • 6. Write a function integerdivision that takes two input numbers and divides the first one with the second one. Question 2 a) Create a vector A with all integers between 5 and 400. 4 Marks ) Give...

    6. Write a function integerdivision that takes two input numbers and divides the first one with the second one. Question 2 a) Create a vector A with all integers between 5 and 400. 4 Marks ) Given a matrix Z, how to access element of the matrix. c) What is the output of this MATLAB code? A [2,4,10,13;16,3,7,18, 8,4,9,25:3,12,15,17); length(A) size(A) Ans) Ans) 6. Write a function integerdivision that takes two input numbers and divides the first one with the...

  • MATLAB: write a function that retuns logical true of vector or matrics or scaler is empty...

    MATLAB: write a function that retuns logical true of vector or matrics or scaler is empty Write a function that retuns logical true of vector or matrics or scaler is empty %% P2: Check for an empty matrix % Write a function myIsEmpty which takes one input (scalar, vector, matrix, ...) % and returns logical true if the input is empty and false otherwise. % DO NOT USE MATLAB's isempty FUNCTION! % % Example: An input of [] should result...

  • What would the function look like in MATLAB? Write a function called mysort that takes a...

    What would the function look like in MATLAB? Write a function called mysort that takes a 3-element vector as its sole arguments. It uses if statements, possibly nested, to return a 3-element vector with its elements in non-decreasing order, i.e., the first element in the returned vector equals the smallest element of the input vector and the last element equals the largest element in the input vector. NOTE: Your function should not use any built-in functions, e.g., sort, min, max,...

  • 1. Write a MATLAB function that takes a matrix, a row number and a scalar as...

    1. Write a MATLAB function that takes a matrix, a row number and a scalar as arguments and multiplies each element of the row of the matrix by the scalar returning the updated matrix. 2. Write a MATLAB function that takes a matrix, two row numbers and a scalar as arguments and returns a matrix with a linear combination of the rows. For example, if the rows passed to the function were i and j and the scalar was m,...

  • 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.

  • [MATLAB] Write a function called myMultProd.m that computes the cumulative product of the elements in a...

    [MATLAB] Write a function called myMultProd.m that computes the cumulative product of the elements in a vector. The cumulative product, pj, of the jth element of the vector x, xj, is defined by pj = (x1)(x2) … (xj) for j = 1:length of the vector x. DO NOT USE CUMPROD For example, when you run your function it should look like this: >> x = [2 3 4 2]; >> myMultProd(x) >> ans = 2 6 24 48 That is,...

  • Write a function called ''minandmax'' . Function ''minandmax'' takes only one input argument that is a...

    Write a function called ''minandmax'' . Function ''minandmax'' takes only one input argument that is a matrix M . This matrix M can be any size (any number of columns and rows) with any random integer values . Function ''minandmax'' returns two output arguments. The first output argument is called ''difference_row'' that returns a row vector containing the absolute values of the difference between the maximum and minimum valued elements in each row. The second output argument is called ''difference_all''...

  • Using matlab Write a function that receives a vector as an input argument and prints the...

    Using matlab Write a function that receives a vector as an input argument and prints the individual elements from the vector in a sentence format. One example of calling this function on the command window looks like >>printvec([1.12 23 9.245]) Element 1 is 1.12 Element 2 is 23.00 Element 3 is 9.25 1. (Note: your function should work for any vector.)

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