how to calculate a combination of multiple arrays

2 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 9 May 2015
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.
  2 Comments
Nilgun Turkileri
Nilgun Turkileri on 9 May 2015
Thank you so much for the quick reply!
This was exactly what I was looking for.
Thanks,
Nil

Sign in to comment.

More Answers (0)

Categories

Find more on Computational Geometry in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!