Results 1 to 4 of 4

Math Help - Matlab questions

  1. #1
    Newbie
    Joined
    Aug 2009
    From
    United Kingdom
    Posts
    17

    Matlab questions

    Hi there,

    I'm stuck on a few questions regarding Matlab programming and would be very appreciative if somebody could help and teach as I'm quite new to it.

    1) Given the following operations in Matlab:

    >> vec = [6 2 4 1 5 3];
    >> idx1 = [0 0 1 1 0 0];
    >> idx2 = idx1 == 1;

    a) write the values in idx2
    b) what is the difference between idx1 and idx2?
    c) what is the result of the following assignments? Explain why.
    A, >> vec(idx1)
    B, >> vec(idx2)

    2) A Matlab function requires that an error be generated if an input vector, named vec, contains any element with a value equal to zero. Implement compactly. You may assume the input has already been checked to determine if it is a vector and that it is numeric.

    3) There are problems with the following Matlab code.

    function vout process(vin, sp, t, pos)
    error(nargchk(0,3,nargin))

    What is wrong with this code? Write correct version.





    Would be very appreciative if somebody could help me and also point me in the direction of Matlab tutorials/help/function writing.

    Regards,
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by rel85 View Post
    Hi there,

    I'm stuck on a few questions regarding Matlab programming and would be very appreciative if somebody could help and teach as I'm quite new to it.

    1) Given the following operations in Matlab:

    >> vec = [6 2 4 1 5 3];
    >> idx1 = [0 0 1 1 0 0];
    >> idx2 = idx1 == 1;

    a) write the values in idx2
    b) what is the difference between idx1 and idx2?
    c) what is the result of the following assignments? Explain why.
    A, >> vec(idx1)
    B, >> vec(idx2)
    Type the commands into Matlab and see what you get.

    Look at the workspace panel of the Matlab console to see what the difference is between idx1 and idx2 (I will tell you idx2 is a logical array so vec(idx2) is the array of elements of vec for which idx2 is true.

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Aug 2009
    From
    United Kingdom
    Posts
    17
    Quote Originally Posted by CaptainBlack View Post
    Type the commands into Matlab and see what you get.

    Look at the workspace panel of the Matlab console to see what the difference is between idx1 and idx2 (I will tell you idx2 is a logical array so vec(idx2) is the array of elements of vec for which idx2 is true.

    CB
    Thanks CaptainBlack, I think I've got most of them now... Any idea as to what the last one is though?

    Also:

    (c) Write a Matlab function to count the number of positive values in an
    input array.
    (d) Write a Matlab function that returns
    true if a matrix contains only
    non-zero elements.
    (e) Write a Matlab function that returns
    true is an input argument is not
    a numerical array.
    (f) There are problems with the following Matlab code.

    ans = 5 + 10;
    6 + 1;
    % Must loop for exactly 15 elements (use ans)
    for k = 0:1:ans-1
    vec[k] = k;
    What is wrong with this code?


    Regards,
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by rel85 View Post
    Thanks CaptainBlack, I think I've got most of them now... Any idea as to what the last one is though?
    I am not presently at a machine with Matlab installed so can't check, but:

    Code:
    function vout process(vin, sp, t, pos)
    error(nargchk(0,3,nargin))
    is missing an "=" between vout and process, also vout seems to be unassigned.

    I cant check error(nargchk(0,3,nargin)) because I would need the matlab help system.

    type "help error" and "help nargchk" at the command prompt.

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Hello - few questions - matlab
    Posted in the Math Software Forum
    Replies: 0
    Last Post: May 4th 2009, 06:49 AM
  2. Basic MATLAB questions
    Posted in the Math Software Forum
    Replies: 3
    Last Post: May 1st 2009, 04:15 AM
  3. Couple Standard Matlab Questions
    Posted in the Math Software Forum
    Replies: 0
    Last Post: November 21st 2008, 07:45 AM
  4. Replies: 4
    Last Post: July 19th 2008, 08:18 PM
  5. Replies: 3
    Last Post: August 1st 2005, 02:53 AM

Search Tags


/mathhelpforum @mathhelpforum