random matrix with a minimum number difference for adjacent numbers accepted
Show older comments
Dear all,
I come to you with a problem I am facing right now. I am trying to make a random matrix with adjacent numbers that have to be at leat min_diff different from one another.
For now, I tried making the code below, but it seem to be flawed as I found 5.3268 5.2605 as adjacent numbers in my last try.
max=6;
min=3;
bounds=[min,max];
min_diff=0.1;
X=zeros(1,1250);
R=rand(1,size(X,2))*range(bounds)+bounds(1);
R1=zeros(size(R));
tcount=0;%just to know the number of itteration
for i=1:length(R)
R1(:,i)=ismembertol(R(:,i),R(:,i-1>0),min_diff);
while R1(:,i)==1
tcount=tcount+1;
R(:,i)=R(:,i)+min_diff;
R1(:,i)=ismembertol(R(:,i),R(:,i-1>0),min_diff);
end
end
Thank you in advance for your help,
JdC
Accepted Answer
More Answers (1)
Why not as follows?
min_diff=0.1;
R=rand(1,6);
R=R./min(abs(diff(R)))*min_diff;
abs(diff(R))
2 Comments
JdC
on 19 Jan 2022
Categories
Find more on Get Started with MATLAB 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!