From: us on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hvso4g$589$1(a)fred.mathworks.com>...
> "us " <us(a)neurol.unizh.ch> wrote in message <hvsnjk$1ju$1(a)fred.mathworks.com>...
> > haithem <haithem.haggui(a)gmail.com> wrote in message <ae397350-a6bc-4935-8d00-3beb656223b0(a)g19g2000yqc.googlegroups.com>...
> > > Hi, all.
> > >
> > > My problem is that I want to generate a sparse vector with a fixed
> > > number of non zero elements randomly distributed.
> > >
> > > Thanks for all
> >
> > one of the many (other) solutions
> >
> > n=10;
> > nr=2;
> > s=spalloc(1,10,10);
> > rp=randperm(n);
> > s(rp(1:nr))=1;
> >
> > us
>
> The problem is for large sparse matrix size such as m x n = 1e6 x 1e6, generate a randperm of size 1e12 is a no-no.
>
> Bruno

ok...
a pedestrian work-around might look like this

ns=10; % <- an arbitrary value >> 1...
rp=unique(ceil(n*rand(1,ns*nr))); % <- changes are slim they are all equal...
s(rp(1:nr))=1;

us
From: Bruno Luong on
"us " <us(a)neurol.unizh.ch> wrote in message <hvsp6q$d4k$1(a)fred.mathworks.com>...
> "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hvso4g$589$1(a)fred.mathworks.com>...
> > "us " <us(a)neurol.unizh.ch> wrote in message <hvsnjk$1ju$1(a)fred.mathworks.com>...
> > > haithem <haithem.haggui(a)gmail.com> wrote in message <ae397350-a6bc-4935-8d00-3beb656223b0(a)g19g2000yqc.googlegroups.com>...
> > > > Hi, all.
> > > >
> > > > My problem is that I want to generate a sparse vector with a fixed
> > > > number of non zero elements randomly distributed.
> > > >
> > > > Thanks for all
> > >
> > > one of the many (other) solutions
> > >
> > > n=10;
> > > nr=2;
> > > s=spalloc(1,10,10);
> > > rp=randperm(n);
> > > s(rp(1:nr))=1;
> > >
> > > us
> >
> > The problem is for large sparse matrix size such as m x n = 1e6 x 1e6, generate a randperm of size 1e12 is a no-no.
> >
> > Bruno
>
> ok...
> a pedestrian work-around might look like this
>
> ns=10; % <- an arbitrary value >> 1...
> rp=unique(ceil(n*rand(1,ns*nr))); % <- changes are slim they are all equal...
> s(rp(1:nr))=1;

This is almost SPRAND does. Changes are indeed slim unfortunately slim is not nil.

We could over fill then trim down as John has suggested, but strim down is also costly:

m=1e6;
n=1e6;
nz=1e6;

S = sparse([], [], [], m, n);
while nnz(S) < nz
p = round(1.1*nz) - nnz(S); % 10 % overfilled
i = ceil(m*rand(p,1));
j = ceil(n*rand(p,1));
S = S + sparse(i, j, 1, m, n);
end

if nnz(S) > nz
[i j] = find(S);
p = randperm(length(i));
i = i(p(1:nz));
j = j(p(1:nz));
S = sparse(i, j, 1, m, n);
end

Bruno
From: us on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hvsq2g$8or$1(a)fred.mathworks.com>...
> "us " <us(a)neurol.unizh.ch> wrote in message <hvsp6q$d4k$1(a)fred.mathworks.com>...
> > "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hvso4g$589$1(a)fred.mathworks.com>...
> > > "us " <us(a)neurol.unizh.ch> wrote in message <hvsnjk$1ju$1(a)fred.mathworks.com>...
> > > > haithem <haithem.haggui(a)gmail.com> wrote in message <ae397350-a6bc-4935-8d00-3beb656223b0(a)g19g2000yqc.googlegroups.com>...
> > > > > Hi, all.
> > > > >
> > > > > My problem is that I want to generate a sparse vector with a fixed
> > > > > number of non zero elements randomly distributed.
> > > > >
> > > > > Thanks for all
> > > >
> > > > one of the many (other) solutions
> > > >
> > > > n=10;
> > > > nr=2;
> > > > s=spalloc(1,10,10);
> > > > rp=randperm(n);
> > > > s(rp(1:nr))=1;
> > > >
> > > > us
> > >
> > > The problem is for large sparse matrix size such as m x n = 1e6 x 1e6, generate a randperm of size 1e12 is a no-no.
> > >
> > > Bruno
> >
> > ok...
> > a pedestrian work-around might look like this
> >
> > ns=10; % <- an arbitrary value >> 1...
> > rp=unique(ceil(n*rand(1,ns*nr))); % <- changes are slim they are all equal...
> > s(rp(1:nr))=1;
>
> This is almost SPRAND does. Changes are indeed slim unfortunately slim is not nil.

of course...
however, i wonder - for practical reasons - how slimly slim approaches nil; i guess: nil...

us
From: Bruno Luong on
"us " <us(a)neurol.unizh.ch> wrote in message <hvsrh8$c47$1(a)fred.mathworks.com>...
> "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message
>
> of course...
> however, i wonder - for practical reasons - how slimly slim approaches nil; i guess: nil...
>

If I might give a suggestion us, when talking to women please don't mix between "you are slim" and "you are nil".

Bruno
From: haithem on
On 23 juin, 06:28, "us " <u...(a)neurol.unizh.ch> wrote:
> haithem <haithem.hag...(a)gmail.com> wrote in message <ae397350-a6bc-4935-8d00-3beb65622...(a)g19g2000yqc.googlegroups.com>...
> > Hi, all.
>
> > My problem is that I want to generate a sparse vector with a fixed
> > number of non zero elements randomly distributed.
>
> > Thanks for all
>
> one of the many (other) solutions
>
>      n=10;
>      nr=2;
>      s=spalloc(1,10,10);
>      rp=randperm(n);
>      s(rp(1:nr))=1;
>
> us

It's exactly what I need!!

Many thanks