How can i generate an array of integers that is more dense around a certain value?
Show older comments
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 Comments
Why not something like,
x=min(max(poissrnd(30,1e6,1),1),100);
histogram(x,100); xlim([1,100])
Camilla Ancona
on 27 Jun 2023
Moved: Matt J
on 27 Jun 2023
John D'Errico
on 27 Jun 2023
Highly confusing. You want a vector that is linearly spaced, BUT is denser in some places than in others???? As much as I want to offer a solution, that makes little sense. Entirely integer, and you know how many elements you want to produce? No matter what you do, this will fail, SOME of the time.
the cyclist
on 27 Jun 2023
I'm confused, too.
Could you give a few examples of vectors that are linearly spaced integers in the range 1:20 and are more dense around 5?
Do you mean something like
- [2,3,4,5,6]
- [3,4,5,6,7]
- [4,5,6,7,8]
would occur more frequently than
- [12,13,14,15,16]
- [13,14,15,16,17]
- [14,15,16,17,18]
?
Camilla Ancona
on 27 Jun 2023
Steven Lord
on 27 Jun 2023
You've stated what you're hoping the code will do. But let's take a step back. Can you explain (in words not in code) what problem you're trying to solve with this integer valued array? What about the problem you're trying to solve requires that array to be more dense in some areas (and so less in others)? I can think of a couple potential reasons, but I'd like to hear what you have in mind.
Camilla Ancona
on 27 Jun 2023
Steven Lord
on 27 Jun 2023
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.)
Accepted Answer
More Answers (1)
John D'Errico
on 27 Jun 2023
0 votes
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.
Categories
Find more on Creating and Concatenating Matrices 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!
