Info

This question is closed. Reopen it to edit or answer.

How do I take create a vector from the result of a random function every time it goes through the loop?

1 view (last 30 days)
So I have code currently that makes each row of my matrix contain a "1", randomly placed.
A=zeros(300,2000);
n=1;
while n<301
m = randi([1 2000],1,1);
A(n,m)=1;
n=n+1;
end
How do I create a vector 'b' that takes 'm' each time and adds that new random number to the vector? I tried to pre-allocate the space for b, but it still doesn't work. What's wrong?
A=zeros(300,2000);
n=1;
b=zeros(300,1); %preallocation
while n<301
b(m) = randi([1 2000],1,1);
A(n,m)=1;
n=n+1;
end

Answers (2)

dpb
dpb on 29 Jul 2015
Edited: dpb on 29 Jul 2015
First part --
nRow=300; nCol=2000; % define problem size
A(nRow,nCol)=0; % preallocate
iy=randperm(nCol); % columns to populate
A(sub2ind(size(A),1:nRow,iy(1:nRow)))=1; % fill random columns
Not positive what you're trying to do on the latter stage; you never use b and m is whatever it was the last time through the previous loop and never changes afterwards.
If you're trying to add another '1' to the existing A without regard to whether you might pick the same point or not, simply use the same logic as above a second time. To sample without replacement (iow, ensure don't generate the same index again so will always have the same number of ones in each row) use the next nRow values from the index vector iy on each subsequent iteration. IOW, for the second pass,
i1=nRow+1; i2=2*nRow; % start/stop indices for 2nd independent set w/o replacement
A(sub2ind(size(A),1:nRow,iy(i1:i2)))=1;
It should be obvious how to generalize the above for further iterations.
  2 Comments
Guillaume
Guillaume on 29 Jul 2015
@dpb, I'm fairly certain that you meant to use sub2ind instead of ind2sub.
I'm not sure that the OP is looking for uniqueness on the columns.
dpb
dpb on 29 Jul 2015
Edited: dpb on 29 Jul 2015
Yep, thanks. Fixed up the answer for the inadvertent swap of direction...
That's the first alternative but if don't have that constraint there can be a different number of ones per row--OP will have to determine what constraints there are (or aren't), we can't tell...but I'd not be surprised if OP hadn't actually thought about the fact it could happen and what should do if it does.

Guillaume
Guillaume on 29 Jul 2015
Edited: Guillaume on 29 Jul 2015
Firstly, for loops and while loops are equivalent but, in your case, a for loop would be a bit more obvious:
n = 1;
while n < 301
...
n = n + 1;
end
vs
for n = 1:300
...
end
Certainly less to type with for and no mental jump from endpoint to endpoint+1 or forgetting to write the incrementation. Less risk of a bug.
Anyway, if I understood what you're trying to do, the content of the loop should be:
b(n) = randi([1 2000],1,1); %not sure why you thought b(m) made sense
A(n, b(n)) = 1; %particularly since you never create m.
Of course, the whole lot could be vectorised using sub2ind and replaced by these 3 lines:
A=zeros(300,2000);
columns = randi([1 size(A, 2)], 1, size(A, 1));
A(sub2ind(size(A), 1:size(A, 1), columns)) = 1;

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!