# how to write general code for generating points

2 views (last 30 days)
imola on 12 Feb 2015
Edited: imola on 17 Feb 2015
Dear All,
I'm trying to write the following code in general way for dimension >=2, the code is
m=4;
LB=[1 2 3 4]'; UB=[10 20 30 40]';
R=0.25*UB;
X=[];
for I=1:m
x1 = LB(I,:)+(UB(I,:)-LB(I,:))*rand(500,1);
idx = (x1 > LB(I,:)+R (I,:)) & (x1 < UB(I,:)-R(I,:));
X=[X x1];
x1(idx) = [];
end
plot(X(:,1),X(:,2), '.r');

Sara on 12 Feb 2015
You can write the for loop in the following way:
for I=1:m
x1 = LB(I)+(UB(I)-LB(I))*rand(500,1);
X = [X x1];
x1(x1 > LB(I)+R(I) & x1 < UB(I)-R(I)) = [];
end
It is not clear to me why X = [X x1]; is before the assignment x1(...) = []; though. You may have to switch those lines
Sara on 12 Feb 2015
Of course you have the same figure: the X array contains all the x1 value and not only those selected with your criterion. That's why I suggested that you switch the lines: as they are, it makes no sense. Try this:
m=4;
LB=[1 2 3 4]'; UB=[10 20 30 40]';
R=0.25*UB;
X=[];
Xor = [];
for I=1:m
x1 = LB(I)+(UB(I)-LB(I))*rand(500,1);
Xor = [Xor;x1];
x1(x1 > LB(I)+R(I) & x1 < UB(I)-R(I)) = [];
X = [X;x1];
end
plot(Xor, 'ob');hold on % shows all the points initially calc
plot(X, '.r'); % shows only the selected values
You have not assigned any value to Y as in the example you reported, so the plot command cannot have two inputs.