From: John Smith on 21 Apr 2010 13:35 Hi, I need some help with vectors in Matlab. Here is the scenario I have a vector A with 100 elements. From previous calculations, x of the elements are 1 and the rest (100-x) are zero. (I know there are more 1's than zeros in the vector) 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 x-50 (out of x currently in the vector) 1's into zero and I have to do it by randomly converting some 1's to zeros. Can anyone help me with this problem? Thanks, John.
From: us on 21 Apr 2010 13:47 "John Smith" <matlab.help22(a)gmail.com> wrote in message <hqncop$mas$1(a)fred.mathworks.com>... > Hi, > > I need some help with vectors in Matlab. Here is the scenario > I have a vector A with 100 elements. From previous calculations, x of the elements are 1 and the rest (100-x) are zero. (I know there are more 1's than zeros in the vector) > 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 x-50 (out of x currently in the vector) 1's into zero and I have to do it by randomly converting some 1's to zeros. Can anyone help me with this problem? > > Thanks, > John. one of the many solutions n=100; v=[zeros(1,.5*n),round(rand(1,.5*n))]; rp=randperm(n); v=v(rp); us
From: someone on 21 Apr 2010 14:15 "John Smith" <matlab.help22(a)gmail.com> wrote in message <hqnd09$qj5$1(a)fred.mathworks.com>... > Hi, > > I need some help with vectors in Matlab. Here is the scenario > I have a vector A with 100 elements. From previous calculations, x of the elements are 1 and the rest (100-x) are zero. (I know there are more 1's than zeros in the vector) > 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 x-50 (out of x currently in the vector) 1's into zero and I have to do it by randomly converting some 1's to zeros. Can anyone help me with this problem? > > Thanks, > John. Without actually writing out the code, I would do something like: 1. Use find to get the indicies of all the 1's in vector A (there are x of them). 2. Use randperm(x) to generate a random sequence of those elements. 3. Flip the first (x-50) of the elements in the randperm generated sequence to zero. The only tricky thing is to match the indicies of randperm(x) with the indicies of the original vector A. Just be diligent. There are probably more efficient ways, but this method should work.
From: John Smith on 21 Apr 2010 14:27 "us " <us(a)neurol.unizh.ch> wrote in message <hqndmo$b4a$1(a)fred.mathworks.com>... > "John Smith" <matlab.help22(a)gmail.com> wrote in message <hqncop$mas$1(a)fred.mathworks.com>... > > Hi, > > > > I need some help with vectors in Matlab. Here is the scenario > > I have a vector A with 100 elements. From previous calculations, x of the elements are 1 and the rest (100-x) are zero. (I know there are more 1's than zeros in the vector) > > 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 x-50 (out of x currently in the vector) 1's into zero and I have to do it by randomly converting some 1's to zeros. Can anyone help me with this problem? > > > > Thanks, > > John. > > one of the many solutions > > n=100; > v=[zeros(1,.5*n),round(rand(1,.5*n))]; > rp=randperm(n); > v=v(rp); > > us Thanks for your reply. However, when I run the code, it doesn't work. The number of one's and zero's remain the same, just the order changed somewhat. What I need to do it that: Assume, i have 80 1's and 20's zeros in my vector. I want to turn the 30 1's (out of 80's I have) to zero so that there are equal number of zeros and ones. I Hope you will be able to help me with this. Thanks again.
From: Bruno Luong on 21 Apr 2010 14:40
Excellent suggestion from "someone": A = rand(1,100)>0.3 % Engine idx = find(A); p = randperm(length(idx)); A(idx(p(1:length(idx)-50)))=0; % Bruno |