Results 1 to 2 of 2

Math Help - Matlab function M-Files

  1. #1
    Newbie
    Joined
    Apr 2010
    Posts
    3

    Matlab function M-Files

    hi, im new to this software and need some help for these following questions:

    the 1st question is to define a user defined function randVector(d,n) which returns a vector of d rows filled with zeros, except for n randomly placed non-zero elements (any random value). this is what i've done:

    function e=randVector(d,n)
    r=rand(n,1);
    e=zeros(d,1);
    a=[r;zeros(d-n,1)];
    s=randperm(d);
    for i=1:d
    e(i)=a(s(i));
    end
    end

    im stuck with the 2nd one:
    Use a conditional if statement and create vector M=randVector(j+2,k+1), for k<j and j, k are the first and the last element of x respectively. (x is a given vector of 6 digits )

    thanks
    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 boredmonkey View Post
    hi, im new to this software and need some help for these following questions:

    the 1st question is to define a user defined function randVector(d,n) which returns a vector of d rows filled with zeros, except for n randomly placed non-zero elements (any random value). this is what i've done:

    function e=randVector(d,n)
    r=rand(n,1);
    e=zeros(d,1);
    a=[r;zeros(d-n,1)];
    s=randperm(d);
    for i=1:d
    e(i)=a(s(i));
    end
    end

    im stuck with the 2nd one:
    Use a conditional if statement and create vector M=randVector(j+2,k+1), for k<j and j, k are the first and the last element of x respectively. (x is a given vector of 6 digits )

    thanks
    Code:
    j=x(1);k=x(length(x));
    if k<j
      M=randVector(j+2,k+1)
    else
      M=[]
    end
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 2
    Last Post: February 19th 2011, 06:54 AM
  2. Replies: 8
    Last Post: June 10th 2010, 04:36 AM
  3. MATLAB - Function M-Files
    Posted in the Math Software Forum
    Replies: 6
    Last Post: March 27th 2010, 11:20 PM
  4. Play .avi files in MATLAB GUI?
    Posted in the Math Software Forum
    Replies: 1
    Last Post: November 24th 2009, 06:06 PM
  5. Matlab, vectors and function files
    Posted in the Math Software Forum
    Replies: 2
    Last Post: August 20th 2009, 02:41 PM

Search Tags


/mathhelpforum @mathhelpforum