Linear combination of two single column matrix

I get two matrix :
A =
1
2
3
B =
12
13
14
i want to create C with this inside :
C =
1 2 3 4 5 6 7 8 9 10 11 12
2 3 4 5 6 7 8 9 10 11 12 13
3 4 5 6 7 8 9 10 11 12 13 14
the only way i succeed to do that is with the following code :
for i= 1,69;
C (i,:) = linspace( A(i),B(i),12 )
end
i'm looking for a non-loop solution. is there any one ?
thanks

Answers (2)

C = bsxfun(@plus,A,0:mean(B-A))
or
n = B-A;
C = bsxfun(@plus,A,0:n(1))
or
C = bsxfun(@plus,A-1,A(1):B(1))
or
n = 12
bsxfun(@plus,A,(B - A)/(n-1)*(0:n-1))
LINSPACE does not do any complicated stuff. Therefore it is easy to adjust:
function C = linspaceVector(A, B, n)
A = A(:);
B = B(:);
C = [bsxfun(@plus, A, (B - A) * ((0:n-2) ./ (floor(n) - 1))), B];

1 Comment

Hi Jan.
bsxfun(@plus,A,cumsum((B - A)/(n-1)*[0 ones(1,n-1)],2))
or
cumsum([A (B - A)/(n-1)*ones(1,n-1)],2)

Sign in to comment.

Categories

Asked:

on 21 Oct 2011

Community Treasure Hunt

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

Start Hunting!