Randomization one of four triangles - Error: index out of bounds

Here is my code:
downtri = [50 0;0 50;-50 0];
uptri = [50 0;0 -50;-50 0];
righttri = [0 50;0 -50;50 0];
lefttri = [0 50;0 -50;-50 0];
arrarray = [downtri;uptri:lefttri:righttri];
arrdx1 = randi(4,1,1);
arrow1 = arrarray(arrdx1,:);
arrdx2 = randi(4,1,1);
while ismember(arrdx2, arrdx1)
arrdx2 = randi(4,1,1);
end
arrow2 = arrarray(arrdx2,:);
arrdx3 = randi(4,1,1);
while ismember(arrdx3, [arrdx1,arrdx2])
arrdx3 = randi(4,1,1);
end
arrow3 = arrarray(arrdx3,:);
arrdx4 = randi(4,1,1);
while ismember(arrdx4, [arrdx1,arrdx2,arrdx3])
arrdx4 = randi(4,1,1);
end
arrow4 = arrarray(arrdx4,:);
So the above is meant to randomize shape of a triangle with 4 different shape possibilities (it works great with randomizing color). However, when I try to randomize triangles I get this error message: "Attempted to access arrarray(4,:); index out of bounds because size(arrarray)=[3,2]. I think it means that basically the shape of the triangle won't fit into the matrix, but I'm not sure how to fix it.
Please help, :( :( :(

 採用された回答

Walter Roberson
Walter Roberson 2012 年 11 月 24 日
編集済み: Walter Roberson 2012 年 11 月 24 日

0 投票

Change
arrarray = [downtri;uptri:lefttri:righttri];
to
arrarray = [downtri;uptri;lefttri;righttri];
Warning: your triangles are all 3 x 2, and you are trying to store several of them in one array, but then you are accessing by row number rather than by triangle number.
Side note:
Have you considered replacing your code
arrdx4 = randi(4,1,1);
while ismember(arrdx4, [arrdx1,arrdx2,arrdx3])
arrdx4 = randi(4,1,1);
end
with
arrdx4 = setdiff(1:4, [arrdx1,arrdx2,arrdx3]);
?

4 件のコメント

Brett
Brett 2012 年 11 月 24 日
What would be the advantage of changing the code to setdiff? It appears to be randomizing now, but now it won't display because the matrix dimensions do not agree for the repmat code:
ty = 262
lx = 390
uptriq1 = arrow4 + repmat([lx, ty], 3, 1)
Screen('FillPoly', window, [255 255 255], uptriq4)
What type of matrix dimensions do I need?
Walter Roberson
Walter Roberson 2012 年 11 月 24 日
You are accessing your matrix by row number rather than by triangle number. Your arrow is becoming 1 by something when you do not want it to be.
The choice of the last of your triangles is completely determined once the first three are known, so there is no point in doing randomization: you just waste time looping around until randomly the value becomes the one value that is still left. Might as well just use that value directly. Just a matter of efficiency. Randomizing to find the last value could take an indefinite time.
Brett
Brett 2012 年 11 月 24 日
Do you have any idea might access by triangle number? If I used something like:
random = rand(1)
if random <(1/4)
triangle1 = [50 0;0 50;-50 0]
elseif random>=(1/4) && random<(2/4)
...
Do you think that would fix the problem?
Walter Roberson
Walter Roberson 2012 年 11 月 25 日
Consider going 3 dimensional. One dimension for x, one dimension for y, one dimension for triangle number.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by