From: sutisna on
Hi All,
Basically I'm writing a program to simulate memory testing. I'm told
to create 100 random binary vectors of size 1024 with exactly 50 of
the elements set to 1(random binary). Then I'm asked to create a
column vector called willshawnet of size 1024, with 50% of it is 1.
50% of 1024 is 512.Then I have to train my network with the random
binary patterns I have created. Training mean if random(1) = 1 then I
set willshaw(1). By doing this the sum of willshaw will be greater
than 512. Next, I will have to restore the will shaw network condition
to 50%. This means I have to select any random cell in the willshaw
vector. If it is = 1, then I have to change it to 0. Therefore, I
create a while loop and uses the command random = round(1024*rand(1))
to generate a random number for selecting the cell. I ran my program
then I got the error "??? Subscript indices must either be real
positive integers or logicals." I'm sure that my random number is an
integer and it is greater than 0 and less than 1024.This is the
original code

%% Creating 100 Random Binary with exactly 50 of the elemens of 1 %%
for i = 1:100
randombinary(:,:,i) = [ones(50,1);zeros(974,1)];
randombinary(:,:,i) = randombinary(randperm(1024));
randombinary2(:,:,i) = reshape(randombinary(:,:,i),32,32);
end
%% Willshaw network with a single output outline%%
willshawnet = [ones(512,1);zeros(512,1)]; % 50 percent of 1
willshawnet = willshawnet(randperm(1024)); % random distribution
%% Begin Question 4 %%
for b = 1:100
randombinary(:,:,b);
for cell = 1:1024;
if (randombinary(cell,:,b) == 1)
willshawnet(cell) = 1;
end
end
sumsynapse = sum(willshawnet);
while (sumsynapse > 512)
random = round(rand(1)*1024);
if(willshawnet(random) == 1)
willshawnet(random) = 0;
end
sumsynapse = sum(willshawnet);
end
end

I tried to debug by scraping the loop for accessing the randombinary
matrix one by one, and the program worked fine!
This is what i changed at the end of the script.

randombinary(:,:,1);
for cell = 1:1024;
if (randombinary(cell,:,1) == 1)
willshawnet(cell) = 1;
end
end
sumsynapse = sum(willshawnet);
while (sumsynapse > 512)
random = round(rand(1)*1024);
if(willshawnet(random) == 1)
willshawnet(random) = 0;
end
sumsynapse = sum(willshawnet);
end

As I explained before, I need to train the network with all
randombinary vectors (there are 100s of them), therefore I need to
utilize the loop.

Do any of you have any idea on how to fix this problem? I think it has
something to do with the for loop but I don't know why it is wrong.

Best,
Erry
From: TideMan on
On Apr 14, 2:58 pm, suti...(a)usc.edu wrote:
> Hi All,
> Basically I'm writing a program to simulate memory testing. I'm told
> to create 100 random binary  vectors of size 1024 with exactly 50 of
> the elements set to 1(random binary). Then I'm asked to create a
> column vector called willshawnet of size 1024, with 50% of it is 1.
> 50% of 1024 is 512.Then I have to train my network with the random
> binary patterns I have created. Training mean if random(1) = 1 then I
> set willshaw(1). By doing this the sum of willshaw will be greater
> than 512. Next, I will have to restore the will shaw network condition
> to 50%. This means I have to select any random cell in the willshaw
> vector. If it is = 1, then I have to change it to 0. Therefore, I
> create a while loop and uses the command random = round(1024*rand(1))
> to generate a random  number for selecting the cell. I ran my program
> then I got the error "??? Subscript indices must either be real
> positive integers or logicals." I'm sure that my random number is an
> integer and it is greater than 0 and less than 1024.This is the
> original code
>
> %% Creating 100 Random Binary with exactly 50 of the elemens of 1 %%
> for i = 1:100
> randombinary(:,:,i) = [ones(50,1);zeros(974,1)];
> randombinary(:,:,i) = randombinary(randperm(1024));
> randombinary2(:,:,i) = reshape(randombinary(:,:,i),32,32);
> end
> %% Willshaw network with a single output outline%%
> willshawnet = [ones(512,1);zeros(512,1)]; % 50 percent of 1
> willshawnet = willshawnet(randperm(1024)); % random distribution
> %% Begin Question 4 %%
>  for b = 1:100
>    randombinary(:,:,b);
>     for cell = 1:1024;
>     if (randombinary(cell,:,b) == 1)
>         willshawnet(cell) = 1;
>     end
>     end
>       sumsynapse = sum(willshawnet);
>     while (sumsynapse > 512)
>        random = round(rand(1)*1024);
>       if(willshawnet(random) == 1)
>        willshawnet(random) = 0;
>       end
>       sumsynapse = sum(willshawnet);
>     end
>  end
>
> I tried to debug by scraping the loop for accessing the randombinary
> matrix one by one, and the program worked fine!
> This is what i changed at the end of the script.
>
>  randombinary(:,:,1);
>     for cell = 1:1024;
>     if (randombinary(cell,:,1) == 1)
>         willshawnet(cell) = 1;
>     end
>     end
>       sumsynapse = sum(willshawnet);
>     while (sumsynapse > 512)
>        random = round(rand(1)*1024);
>       if(willshawnet(random) == 1)
>        willshawnet(random) = 0;
>       end
>       sumsynapse = sum(willshawnet);
>     end
>
> As I explained before, I need to train the network with all
> randombinary vectors (there are 100s of them), therefore I need to
> utilize the loop.
>
> Do any of you have any idea on how to fix this problem? I think it has
> something to do with the for loop but I don't know why it is wrong.
>
> Best,
> Erry

Well, I haven't delved into your code, but I suspect that your problem
could be
random = round(rand(1)*1024);
which will be zero if rand(1) < 0.5/1024
Then, accessing willshawnet(random) would give the error message.
From: sutisna on
On Apr 13, 8:43 pm, TideMan <mul...(a)gmail.com> wrote:
> On Apr 14, 2:58 pm, suti...(a)usc.edu wrote:
>
>
>
>
>
> > Hi All,
> > Basically I'm writing a program to simulate memory testing. I'm told
> > to create 100 random binary  vectors of size 1024 with exactly 50 of
> > the elements set to 1(random binary). Then I'm asked to create a
> > column vector called willshawnet of size 1024, with 50% of it is 1.
> > 50% of 1024 is 512.Then I have to train my network with the random
> > binary patterns I have created. Training mean if random(1) = 1 then I
> > set willshaw(1). By doing this the sum of willshaw will be greater
> > than 512. Next, I will have to restore the will shaw network condition
> > to 50%. This means I have to select any random cell in the willshaw
> > vector. If it is = 1, then I have to change it to 0. Therefore, I
> > create a while loop and uses the command random = round(1024*rand(1))
> > to generate a random  number for selecting the cell. I ran my program
> > then I got the error "??? Subscript indices must either be real
> > positive integers or logicals." I'm sure that my random number is an
> > integer and it is greater than 0 and less than 1024.This is the
> > original code
>
> > %% Creating 100 Random Binary with exactly 50 of the elemens of 1 %%
> > for i = 1:100
> > randombinary(:,:,i) = [ones(50,1);zeros(974,1)];
> > randombinary(:,:,i) = randombinary(randperm(1024));
> > randombinary2(:,:,i) = reshape(randombinary(:,:,i),32,32);
> > end
> > %% Willshaw network with a single output outline%%
> > willshawnet = [ones(512,1);zeros(512,1)]; % 50 percent of 1
> > willshawnet = willshawnet(randperm(1024)); % random distribution
> > %% Begin Question 4 %%
> >  for b = 1:100
> >    randombinary(:,:,b);
> >     for cell = 1:1024;
> >     if (randombinary(cell,:,b) == 1)
> >         willshawnet(cell) = 1;
> >     end
> >     end
> >       sumsynapse = sum(willshawnet);
> >     while (sumsynapse > 512)
> >        random = round(rand(1)*1024);
> >       if(willshawnet(random) == 1)
> >        willshawnet(random) = 0;
> >       end
> >       sumsynapse = sum(willshawnet);
> >     end
> >  end
>
> > I tried to debug by scraping the loop for accessing the randombinary
> > matrix one by one, and the program worked fine!
> > This is what i changed at the end of the script.
>
> >  randombinary(:,:,1);
> >     for cell = 1:1024;
> >     if (randombinary(cell,:,1) == 1)
> >         willshawnet(cell) = 1;
> >     end
> >     end
> >       sumsynapse = sum(willshawnet);
> >     while (sumsynapse > 512)
> >        random = round(rand(1)*1024);
> >       if(willshawnet(random) == 1)
> >        willshawnet(random) = 0;
> >       end
> >       sumsynapse = sum(willshawnet);
> >     end
>
> > As I explained before, I need to train the network with all
> > randombinary vectors (there are 100s of them), therefore I need to
> > utilize the loop.
>
> > Do any of you have any idea on how to fix this problem? I think it has
> > something to do with the for loop but I don't know why it is wrong.
>
> > Best,
> > Erry
>
> Well, I haven't delved into your code, but I suspect that your problem
> could be
> random = round(rand(1)*1024);
> which will be zero if rand(1) < 0.5/1024
> Then, accessing willshawnet(random) would give the error message.

It has nothing to do with the random varialble because when I set
while( sumsynapse < 512) %% less than.. instead of >
random = round(rand(1)*1024);
if(willshawnet(random) == 1)
willshawnet(random) = 0;
end
sumsynapse = sum(willshawnet);
end
The program is working fine, is just that that is not what I want to
do.

Cheers
Erry
From: Walter Roberson on
sutisna(a)usc.edu wrote:

> It has nothing to do with the random varialble because when I set
> while( sumsynapse < 512) %% less than.. instead of >
> random = round(rand(1)*1024);
> if(willshawnet(random) == 1)
> willshawnet(random) = 0;
> end
> sumsynapse = sum(willshawnet);
> end
> The program is working fine, is just that that is not what I want to
> do.

Your counter-example proves nothing. When you run after changing the >
to <, you run a different number of random selections, and so you just
don't happen to encounter the random value that causes the problem.

When you use round(rand(1)*1024) you can potentially get 1025 different
values:
(0, .5) -> 0
[.5, 1.5) -> 1
[1.5, 2.5) -> 2
and so on up to
[1022.5, 1023.5) -> 1023
[1023.5, 1024) -> 1024

1 through 1024 plus the 0 is 1025 different values. The lowest and
highest value are each half of the probabilities of the other values.

I recommend that you instead use

ceil(rand(1)*1024)

as this will generate values from 1 to 1024, with no possibility of 0
(because rand() is guaranteed not to generate exactly 0.)
From: sutisna on
On Apr 13, 9:18 pm, Walter Roberson <rober...(a)hushmail.com> wrote:
> suti...(a)usc.edu wrote:
> > It has nothing to do with the random varialble because when I set
> > while( sumsynapse < 512) %% less than.. instead of >
> >          random = round(rand(1)*1024);
> >       if(willshawnet(random) == 1)
> >       willshawnet(random) = 0;
> >       end
> >       sumsynapse = sum(willshawnet);
> >         end
> > The program is working fine, is just that that is not what I want to
> > do.
>
> Your counter-example proves nothing. When you run after changing the >
> to <, you run a different number of random selections, and so you just
> don't happen to encounter the random value that causes the problem.
>
> When you use round(rand(1)*1024) you can potentially get 1025 different
> values:
> (0, .5) -> 0
> [.5, 1.5) -> 1
> [1.5, 2.5) -> 2
> and so on up to
> [1022.5, 1023.5) -> 1023
> [1023.5, 1024) -> 1024
>
> 1 through 1024 plus the 0 is 1025 different values. The lowest and
> highest value are each half of the probabilities of the other values.
>
> I recommend that you instead use
>
> ceil(rand(1)*1024)
>
> as this will generate values from 1 to 1024, with no possibility of 0
> (because rand() is guaranteed not to generate exactly 0.)

Thank you so much for your help! It works now. I'm glad that I learn
something new everyday.