MATLAB: How to calculate a combination of multiple arrays

combinationcoordinates

Hi,
I would like to randomly assign a coordinate deriving from 4 different arrays such that:
x1=[-400:15:-15]; y1=[-300:30:-30]; x2=[-385:15:0]; y2=[-270:30:0];
In return I would like to have all possible combinations such AX= [x1,y1,x2,y2]: For example: A1= [-400,-300,-385,-270], A2 = [-385,-370,-270,-240],…AN=[-15,-30,0,0] so that I can randomly assign one of them. Though I am not sure if it is possible??
Thank you in advance,
Nil

Best Answer

I would discourage you from creating separate row vectors for each ‘A1’, ‘A2’, etc. Instead, create one matrix and choose the rows you want:
x1=[-400:15:-15]; y1=[-300:30:-30]; x2=[-385:15:0]; y2=[-270:30:0];
Nr = 50;
xi = randi(length(x1),Nr,2);
yi = randi(length(y1),Nr,2);
A = [x1(xi(:,1))' y1(yi(:,1))' x2(xi(:,2))' y2(yi(:,2))'];
This creates a (Nrx4) matrix with the random elements from your four vectors.