Results 1 to 4 of 4

Math Help - Matlab help with Vectors

  1. #1
    Newbie
    Joined
    Apr 2010
    Posts
    2

    Question Matlab help with Vectors

    Hi,

    I need some help with vectors in Matlab.
    I have a vector A with 100 elements. From previous calculations, 80 of the elements are 1 and the rest are zero.
    But I need to make the vector with half of its element "zero" and the remaining half "one" by randomly turning some one's to zero.

    So I need to turn 30 (out of 80 currently in the vector) 1's into zero and I have to do it randomly. Can anyone help me with this problem?

    Thanks,
    John.
    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 john2010 View Post
    Hi,

    I need some help with vectors in Matlab.
    I have a vector A with 100 elements. From previous calculations, 80 of the elements are 1 and the rest are zero.
    But I need to make the vector with half of its element "zero" and the remaining half "one" by randomly turning some one's to zero.

    So I need to turn 30 (out of 80 currently in the vector) 1's into zero and I have to do it randomly. Can anyone help me with this problem?

    Thanks,
    John.
    Try (the untested code):

    Code:
    B=find(A == 1)
    C=zeros(size(A));
    x=randperm(length(B));
    idx=x(1:50);
    C(idx)=1;
    A=C;
    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Apr 2010
    Posts
    2
    Quote Originally Posted by CaptainBlack View Post
    Try (the untested code):

    Code:
    B=find(A == 1)
    C=zeros(size(A));
    x=randperm(length(B));
    idx=x(1:50);
    C(idx)=1;
    A=C;
    CB
    Thanks for the reply. I tested the code and it gives me 50 zero's and 50 one's but the problem is all the 1's bits towards the end of the vector are deleted (instead of being random deletion). I hope you can help me with this. Thanks a lot.
    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 john2010 View Post
    Thanks for the reply. I tested the code and it gives me 50 zero's and 50 one's but the problem is all the 1's bits towards the end of the vector are deleted (instead of being random deletion). I hope you can help me with this. Thanks a lot.
    Code:
    B=find(A == 1)
    C=zeros(size(A));
    x=randperm(length(B));
    idx=B(x(1:50));
    C(idx)=1;
    A=C;
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Creating animated plot of vectors in Matlab
    Posted in the Math Software Forum
    Replies: 1
    Last Post: May 4th 2010, 01:33 AM
  2. Matlab Split vectors,if statements, and "for loop"
    Posted in the Math Software Forum
    Replies: 5
    Last Post: February 27th 2010, 03:30 AM
  3. Matlab, vectors and function files
    Posted in the Math Software Forum
    Replies: 2
    Last Post: August 20th 2009, 02:41 PM
  4. Matlab 7.0 vectors
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: April 6th 2008, 06:55 AM
  5. MATLAB linearly dependant vectors
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 3rd 2008, 03:42 PM

Search Tags


/mathhelpforum @mathhelpforum