MATLAB: Populate matrix columns based on a vector of index vector

column populating

I generate an 8×8 A matrix below. I also have an 8×1 vector B containing random integer numbers from 1 to 8. And, I have an 8×1 C vector containing numbers obtained below. How can I, for all rows of A, populate those columns of A starting at B to the end, with values of corresponding rows in C without going through a loop (i.e. vectorized).
A=rand(8);
B = randi([1 8], 8, 1);
C=-linspace(8,30,8)';

Best Answer

>> X = (1:5)>=B(:); % requires >=R2016b, for earlier versions replace >= with BSXFUN
>> [R,~] = find(X);
>> A(X) = C(R)
A =
-10 -10 -10 -10 -10
1 2 -9 -9 -9
1 2 3 -5 -5
1 -2 -2 -2 -2
1 2 3 4 -1