I'll be editing this answer as I read the code.

---edit: the main reason for your code not working is at the end of the answer. I'm not looking at the rest of the code.

I have only looked at the beginning of the code. Already:

ii=1+round(9*rand);

if ii > 1

else

end

A simpler way to write that is:

ii = randi(10);

if ii > 1

else

end

which has the added benefit of actually giving you a uniform distribution and combined with the if test does give you 90%-10% probability. Your method doesn't, it's 95%-5%

>> histogram(1+round(9*rand(1, 1e5)), 'BinMethod', 'integers', 'Normalization', pdf)

I'll let you work out why it's not uniform at the edges.

--------

direzione=input('direzione? usa WASD','s');

if isempty(direzione)

direzione=input('direzione? usa solo WASD','s');

end

You ask for input, and if it's empty ask again. Why if the user again enter nothing. You continue as if all was well. What if the user did enter something but it's invalid.

The best way to cope with both is with a while loop:

direzione = '';

while ~ismember(direzione, 'wsad')

direzione=lower(input('direzione? usa solo WASD','s'));

end

-------

Major bug there! a1 and a are matrices, so a1~=a is a logical matrix (4x4) of true and false values (0 and 1). What does while consider true when you pass it a logical matrix?

The condition is only true if all the elements of the matrix is true. Therefore, the while loop will only be entered if all the element of a1 are different from a. If even one element of a1 is the same as the corresponding element of a, then the condition is false, and you skip the while.

You actually meant to enter the while if any element of a1 differs from a, you could have written that as:

or simpler not do an element by element comparison and just ask matlab are the matrices the same with isequal:

## Best Answer