1

I'm writing a function in MATLAB that takes in the coordinates x and y of 3 billiard balls, and wants to output which ball has moved first, second and last, and how many balls have moved in total. I came up with a solution, but it involves a significant amount of if-else statements, which seems a bit complicated for the task at hand.

I'm not asking for a solution I can copy-paste, but rather, if you could help guide an inexperienced MATLAB user in a certain direction I would be very grateful.

Here is what my code looks like with only one of the three if else blocks used:

function [FirstBall,SecondBall,LastBall, NbBallsMoved] = GetBallMoveOrder(Xr, Yr, Xy, Yy, Xw, Yw, MoveDistPx)
    [Idx_r,len_r] = GetFirstMoveIdx(Xr,Yr,MoveDistPx);
    [Idx_y,len_y] = GetFirstMoveIdx(Xy,Yy,MoveDistPx);
    [Idx_w,len_w] = GetFirstMoveIdx(Xw,Yw,MoveDistPx);
    if Idx_r < Idx_y && Idx_r < Idx_w       % Cas où le rouge est premier
        FirstBall = 1;
        if Idx_y < Idx_w
            SecondBall = 2;
            LastBall = 3;
        elseif Idx_y > Idx_w
            SecondBall = 3;
            Lastball = 2;
        elseif len_y > len_w
            SecondBall = 2;
            LastBall = 3;
        elseif len_y < len_w
            SecondBall = 3;
            LastBall = 2;
        else 
            SecondBall = 2;
            LastBall = 3;
        end
    end
end

Here Xr and Yr are arrays of coordinates, and the function GetFirstMoveIdx returns the index of the first move, along with the length in pixels of the first move. I tried to use the sort function but didn't get very far, since the sort function mixes up the values where I don't know which value corresponds to which ball.

Wolfie
  • 27,562
  • 7
  • 28
  • 55
yocut
  • 11
  • 2
  • The `sort` function has a second output argument that provides the sort order. You can use that to keep your balls apart. – Cris Luengo Mar 21 '23 at 22:39

1 Answers1

0

It seems your order of balls is matched by Matlab's sortrows function:

[~, Idx_ord] = sortrows([Idx_r len_r; Idx_y len_y; Idx_w, len_w])

[Idx_r len_r; Idx_y len_y; Idx_w, len_w] is a 2x3 matrix you may want to visualize before sorting.

I used tilde there to ignore the first result, the sorted matrix, we just need the indices.

Now we only have to assign the indices to the three variables:

FirstBall = Idx_ord(1);
SecondBall = Idx_ord(2);
LastBall = Idx_ord(3);

or, to assign all three in a single statement, see this post

[FirstBall, SecondBall, LastBall] = deal(num2cell(Idx_ord){:})

Sorry, this is a copy-paste-solution kind of site ;)

kikon
  • 3,670
  • 3
  • 5
  • 20
  • Thank you very much! The thing i need to return the index 1, 2 or 3 which corresponds to the red, yellow or white ball. So the index returned by GetFirstMoveIdx returns the "moment in time" where the ball moves, which here is only relevant for determining which ball moves first, but its value itself is only relevant for sorting, and not for the output of the function – yocut Mar 22 '23 at 09:00