how to write general code for generating points

1 次查看(过去 30 天)
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');
can any one help me and give me an advice please.
thanks in advance.

回答(1 个)

Sara
Sara 2015-2-12
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
  1 个评论
Sara
Sara 2015-2-12
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by