Assigning values for vector with number sequence

5 views (last 30 days)
n - number that I should assign firstly. Any integer from 1 to infinity. you can limit the upper limit of n for convenience.
m = (n^2 + 3n)/2;
r1 - Any positive integer,should be assigned firstly;
r2 - Any positive integer higher than r1, should be assigned firstly;
vector p has m number of members i.e) p=[1:1:m];
And there is a rule like this.
example)
if n=1 -->
p(1)= r1, p(2)= r2
if n=2 -->
p(1)= r1, p(2)= r2,
p(3)= r1+r1, p(4) = r1+r2, p(5)= r2+r2
for n=3 -->
p(1)= r1, p(2)= r2,
p(3)= r1+r1, p(4) = r1+r2, p(5)= r2+r2,
p(6)= r1+r1+r1, p(7)=r1+r1+r2, p(8)= r1+r2+r2, p(9)=r2+r2+r2
... and so on
How can I make this p vector ? (Actually the order of members of p vector can be changed as long as p contains all members for its n and m.
like when n= 3,
p(1)=r1, p(2)=r1+r1, p(3)=r1+r1+r1, p(4)=r2, p(5)=r1+r2, ..... p(9)= r2+r2+r2
  1 Comment
Stephen23
Stephen23 on 15 Jun 2018
@Ju Kim: please do not close questions that have received answers.

Sign in to comment.

Answers (3)

Stephen23
Stephen23 on 15 Jun 2018
Edited: Stephen23 on 15 Jun 2018
All you need is this:
fun = @(n)r1*(n:-1:0)+r2*(0:n);
p = cell2mat(arrayfun(fun,1:n,'uni',0))
And tested (here I wrote my solution on one line):
>> r1 = 2;
>> r2 = 7;
>> n = 3;
>> p = cell2mat(arrayfun(@(n)r1*(n:-1:0)+r2*(0:n),1:3,'uni',0))
p =
2 7 4 9 14 6 11 16 21
>> p(1)= r1; p(2)= r2;
>> p(3)= r1+r1; p(4) = r1+r2; p(5)= r2+r2;
>> p(6)= r1+r1+r1; p(7)=r1+r1+r2; p(8)= r1+r2+r2; p(9)=r2+r2+r2
p =
2 7 4 9 14 6 11 16 21

Udit Dhand
Udit Dhand on 15 Jun 2018
function p=fun(n,r1,r2)
m=(n^2+3*n)/2;
mat=[0 0];
count=1;
p=ones(1,m);
for j=1:n
mat=[mat; r1 r2]; % append r1, r2 to the matrix for every iteration
for i=1:length(mat);
p(count)=sum(mat(1:i,2))+sum(mat(i+1:length(mat),1)); % counts first i values of 2nd column and i+1 to last for 1st one
count=count+1; %update count
end
end
end

Jonathon Gibson
Jonathon Gibson on 15 Jun 2018
Here's how I went about it:
First, we try to find a pattern in the sequence. To make it easier to see, let 1=r1 and 2=r2 and use concatenation instead of addition. Then we have 1, 2, 11, 12, 22, 111, 112, 122, 222, 1111, etc.
So our pattern seems to have two rules, 1) There are only two digits that we can use (in the case 1 and 2) and 2) each digit cannot be larger than the digit to their right (this ordering is arbitrarily chosen from your example, but we need to define some ordering to avoid double counting r1+r2 and r2+r1). So we can write a function that goes through all of the numbers that use only two digits and have non-decreasing digits.
Binary numbers give us our first criteria easily, we just use 0 and 1 instead of 1 and 2, but we have to be careful not to think 0 == 00 because r1 ~= r1 + r1, so we add a '1' to the front to differentiate, e.g. 0 => 10 and 00 => 100, and it is clear 10 ~= 100. So we check every binary number greater than 1 until we have m numbers satisfying our two conditions. 10 => 0 works (remember to strip the leading 1), 11 => 1 works, 100 => 00 works, 101 => 01 works, but 110 => 10 violates condition 2) so we skip it and keep looking.
Now every element of the p vector has a corresponding binary string, e.g p(3) = '00', p(8) = '011', etc. Then all we need to do is convert this string back into sums of r1 and r2. So for every character, if you see a '0' add r1 and if you see a '1' add r2. Do this for every element of p and you'll have your answer!
function p = fun(n,r1,r2)
m = (n^2 + 3*n)/2;
counter = 1;
p = zeros(1,m);
for i = 1:m
isValid = false;
while ~isValid %Find a number matching the regexp 0*1*
counter = counter + 1; %Try the next binary number
base2 = dec2bin(counter);
base2 = base2(2:end); %Strip leading 1 to get 00, 01, etc
isValid = true; %Assume this new number meets the criteria
for idx = 1:(length(base2)-1) %Check that all the 0s are before the 1s
if base2(idx) > base2(idx+1)
isValid = false; %Doesn't meet criteria if a 1 comes before a 0
end
end
end
%Now convert to sums of r1 and r2, e.g 011 => r1 + r2 + r2
p(i) = 0;
for char = base2
if char == '0'
p(i) = p(i) + r1;
end
if char == '1'
p(i) = p(i) + r2;
end
end
end

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!