From: sylar on
Hello

I'm trying to ceate a 3x3 array of random numbers from 1 to 9. I'm using the following code with which I get the error :
Attempted to access r(0,1); index must be a positive integer or logical.

Error in ==> Untitled at 6
while (t == 1 && (k==2 && r(t,k) == r(t,(k-1))) || (k==3 && (r(t,k) ==
r(t,(k-2)) || r(t,k) == r(t,(k-1)))))...


This is the code:

clear all
z = zeros(3);
for t = 1:3
for k= 1:3
r(t,k)= ceil((8*rand-rand)+1);
while (t == 1 && (k==2 && r(t,k) == r(t,(k-1))) || (k==3 && (r(t,k) == r(t,(k-2)) || r(t,k) == r(t,(k-1)))))...
|| ((t == 2 && (k==2 && r(t,k) == r(t,(k-1))) || (k==3 && (r(t,k) == r(t,(k-2)) || r(t,k) == r(t,(k-1)))) || r((t-1),k) == r(t,k)))...
|| ((t == 3 && (k==2 && r(t,k) == r(t,(k-1))) || (k==3 && (r(t,k) == r(t,(k-2)) || r(t,k) == r(t,(k-1)))) || r((t-1),k) == r(t,k) || r((t-2),k) == r (t,k)))
r(t,k) = ceil((8*rand-rand)+1)
end
z(t,k) = r(t,k)
end
end
From: Jan Simon on
Dear Sylar!

> I'm trying to ceate a 3x3 array of random numbers from 1 to 9. I'm using the following code with which I get the error :

> Attempted to access r(0,1); index must be a positive integer or logical.
>
> Error in ==> Untitled at 6
> while (t == 1 && (k==2 && r(t,k) == r(t,(k-1))) || (k==3 && (r(t,k) ==
> r(t,(k-2)) || r(t,k) == r(t,(k-1)))))...

The message is clear:
you try to access r with the indices 0 and 1, but the indices must be positive ever.
E.g. r(t,(k-1)) must fail for k==1 !

Do you want to do something like this?
x = 1:9;
x = reshape(x(randperm(9)), 3, 3)

Good luck, Jan
 | 
Pages: 1
Prev: Relations: Stuck need help
Next: region growing