Reputation: 97
I want to create 4 sets of random values where each set contains values within a specified interval. The sets are suppose to be at a certain distance from each other. I have already done this(code attached) but i think its a bit too long for this job. Please suggest some efficient method to do this.
X = randi([0,125],1,15);
Y = randi([0,125],1,15);
X1 = randi([0,250],1,15);
Y1 = randi([575,875],1,15);
X2 = randi([625,875],1,15);
Y2 = randi([250,500],1,15);
X3 = randi([875,1000],1,5);
Y3 = randi([875,1000],1,5);
X = horzcat(X,X1,X2,X3);
Y=horzcat(Y,Y1,Y2,Y3);
scatter(X,Y,'filled, labeled')
Upvotes: 1
Views: 58
Reputation: 221674
To get some speedup and make the code more compact, here's one -
d0 = randi([0,125],4,15);
d1 = randi([0,250],3,15);
d2 = randi([575, 875],1,15);
X = [d0(1,:), d1(1,:), d1(2,:)+625, d0(3,:)+875];
Y = [d0(2,:), d2, d1(3,:)+250, d0(4,:)+875];
There are basically 3
ranges and that's what we are using to have the random arrays - d0,d1,d2
and then simply slicing, adding in appropriate offsets and horizontally stacking to get the two output arrays.
With 100000
iterations, the timings I get -
------------- Original method ------------------------------
Elapsed time is 1.267933 seconds.
------------- Proposed method ------------------------------
Elapsed time is 1.068410 seconds.
Upvotes: 1