How can i generate an array of integers that is more dense around a certain value?
21 次查看(过去 30 天)
显示 更早的评论
Hi all, I hope that someone could help me. I need to generate a vector of positive integers in a certain range (let say 1 and 100) linearly spaced that is more dense (aka has more points) around a certain value (let say 60).
EDIT: I need this vector as a range for a for loop. Thus it must not include any repetitions.
I tried to code like this but the following code is not very efficient neither robust to changes in the parameter choice:
M = 10000;
ending_point = 0.01*M;
density_sample = 5;
starting_point = 1;
num_roots_gin = 60;
quantity1 = round(0.5*num_roots_gin);
array = [starting_point:density_sample:ending_point];
tol = 3; %must be less than density
[ff,gg] = find(abs(vettore-num_roots_gin)<tol);
array2 = [(num_roots_gin-quantity1):(num_roots_gin+quantity1)];
array_fin = [array(1:ff-1) array2 array(ff+1:end)];
8 个评论
Steven Lord
2023-6-27
So are you calling your optimizer multiple times, first to identify the broad region where your solution lies then later to localize the solution even further? Like using your optimizer to find what state contains the answer, then using it again to find which city in that state has the answer, then what neighborhood in the city, then what street in the neighborhood, etc.?
It's still not completely clear to me over what you're iterating and why those need to be integer values. If what I described in the first paragraph of my comment is close I'd call the optimizer once with a very coarse set of tolerances (say an absolute tolerance of 1) then call it over and over again, using the final result from the previous optimizer call as the initial guess for the next optimizer call with progressively smaller tolerances (switching to an absolute tolerance of 1e-2, then 1e-4, then 1e-8, etc.)
采纳的回答
DGM
2023-6-27
编辑:DGM
2023-6-27
The example you gave doesn't seem to do what you describe. Is this more what you're looking for?
M = 10000;
ending_point = 0.01*M;
starting_point = 1;
density_sample = 5;
num_roots_gin = 60;
% half-width of high-density window
% this is the number of low-density samples on each side of the point of interest (>=1)
halfw = 2;
array = starting_point:density_sample:ending_point; % low-density linear series
[~,gg] = min(abs(array-num_roots_gin)); % find the index of the element closest to target
array2 = array(gg-halfw):array(gg+halfw); % max density linear segment between adjacent elements
array_fin = [array(1:gg-1-halfw) array2 array(gg+1+halfw:end)]
plot(array_fin,'.-'); hold on
yline(num_roots_gin)
This doesn't ensure that the high-density segment is exactly centered on the point of interest, but it's not clear that it's necessary. Also, I haven't done anything to safeguard against the window hanging off the end of the low-density series (causing an indexing error).
更多回答(1 个)
John D'Errico
2023-6-27
Honestly, you could probably do something using an optimization. But using an optimization technique to solve this seems to be the equivalent of using a Mack truck to carry a pea to Boston - wild overkill. I'd formulate an objective function that would push the vector to have as uniform a spacing as possible, but also would "encourage" more points near the target, while also forcing the solution to be entirely integer. UGH. In fact, UGH squared. It just seems like an ugly way to generate a vector that has the properties you want.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!