Question

Question 2. Write a MATLAB Function my-fun that takes as its input a vector of numeric values nums and returns a vector whose values are exactly/only the indicies of nums whose values are non- zero. You may not use any built-in MATLAB functions - i.e. find Sample call(s): CALL 1: my-fun( [8 05 0] ) \% would evaluate [1 3] CALL 2: my-fun ( [0 9 0 1 0] ) \% would evaluate [2 4] Repost pls show clear and correct codes in matlab..
0 0
Add a comment Improve this question Transcribed image text
Answer #1
function v = my_fun(vec)
    for i=1:length(vec) % go through all elements of vec
        if vec(i) ~= 0  % if it's not 0
            v(length(v)+1) = i; % then add it's index to v
        end
    end
end

% testing code
my_fun([8 0 5 0])   
my_fun([0 9 0 1 0])
Add a comment
Know the answer?
Add Answer to:
Repost pls show clear and correct codes in matlab.. Question 2. Write a MATLAB Function my-fun...
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
  • Repost.. electromagnetic fields Im trying to review and learn from my mistake could someone pls help.....

    Repost.. electromagnetic fields Im trying to review and learn from my mistake could someone pls help.. pls dont just give answers pls explain a little bit why u think thats the correct answer..pls be try to be helpful.. Any heplful help would be apprecaited.. ty wi fust aade thun d decoleiate so that itr yels Question 14 (1 point) Choose a correct statement from the following 1) Magnetization vector, M is proportional to H in simple media 2) H vector...

  • use matlab and show all codes and work the question continues from this 4. Write a...

    use matlab and show all codes and work the question continues from this 4. Write a function with header (A, V - myCone (r, h), which outputs the total area A and volume of a cone with base radius r and height h. 5. Write a function - myMatrix (myvec, m, n) which creates an m-by-n matrix A, as in Problem 3, but for arbitrary values of mand n and any length of vector myvec. Hint: the function can use...

  • Question #2 Write a function fun call that has two arguments: f, a function that has...

    Question #2 Write a function fun call that has two arguments: f, a function that has one argument that is a number and num.list that is a list of numbers. The command fun call(f, numlist) should produce a tuple that consists of the values obtained by applying the function f to each of the numbers in num.list in sequence. As an example, the command fun call (abs, [-1, 3, -4.5, 0]) should return the tuple (1, 3, 4.5, 0) since...

  • 1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector...

    1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...

  • Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as...

    Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as output, c, returns the number of zeros at the beginning of v (number of zero elements before any non-zero element). For example, for input (-5, 5, 11] the output would be 0. If the input is [0, 0, 3, 0, 0, 0], the output would be 2. If the input is [0, 0, 0, 0, 7, 4) the output would be 4. 5) Write...

  • Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts...

    Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts a single variable as input i Create a row vector Tthat represents the number of seconds in two minutes, starting ii. Call the function optimist on the vector T, store the result in variable R1 and returns the double of that variable from one and ending at a hundred and twenty v. Create an anonymous function named pessimist, that accepts a single variable as...

  • 2) Write a function stringManip that takes in a character string and returns a character string...

    2) Write a function stringManip that takes in a character string and returns a character string following these rules: a) any vowel (a, e, i, o, u) is replaced by the character '&' b) any numeric character has 1 added to it (if it was 9, it becomes O instead) (str2num() is useful here) c) all lowercase characters become uppercase d) replace the final character with '!' e) append the length of the string before this step to the end...

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

  • write a Matlab program ( solve the three questions). please use array and create your own...

    write a Matlab program ( solve the three questions). please use array and create your own function to check primality Question 1 : [10 points) Write a MATLAB program that will store all the first 1000 prime numbers in an array variable named arr_of_primes. Please note that a prime number is a positive integer that is bigger than or equal to 2 which is divisible only by 1 and itself. Examples of the first 8 primes are: 2, 3, 5,...

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

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