From: William on
I have a vector v with length n

I want to generate matrix m with size a-by-b-by-n such that

m(:,:,i) = v(i)*ones(a,b), for i = 1,2,...,n

Is there any function that can generate this matrix in one line, my code does not allow me to use loop.

Thanks!
From: Matt Fig on
bsxfun(@times,ones(a,b,length(v)),reshape(v,1,1,[]))
From: Steven Lord on

"William " <huyichuan(a)gmail.com> wrote in message
news:hrff58$rum$1(a)fred.mathworks.com...
>I have a vector v with length n
>
> I want to generate matrix m with size a-by-b-by-n such that
>
> m(:,:,i) = v(i)*ones(a,b), for i = 1,2,...,n
>
> Is there any function that can generate this matrix in one line, my code
> does not allow me to use loop.

% sample data
a = 2;
b = 3;
n = 10;
v = 1:n;

M = repmat(reshape(v, [1 1 n]), a, b);

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ