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

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!

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