I have vector of size 25001x1 which represent one of my signal samples and i want to create the matrix such way that first column of the matrix is my vector or signal and the r
The first part of your question is answered in this other Q&A. The fastest method there (not the accepted answer) is as follows:
N = numel(A);
val = repmat([A(:);0],1,N).*bsxfun(@le,[1:N+1]',[N:-1:1]);
out = reshape(val(1:N*N),N,N);
For MATLAB R2016b and newer we can modernize that:
N = numel(A);
val = repmat([A(:);0],1,N) .* ((1:N+1).' <= (N:-1:1));
out = reshape(val(1:N*N),N,N);
(I simply replaced bsxfun(@le,x,y)
with x<=y
, since a few years ago there is no longer a need to use bsxfun
in these cases. I also removed redundant concatenation operators []
, and replaced '
with .'
, which is more correct for this use.)
For the second part of your question, we need to generalize the code above in a non-trivial manner. The following code is the result of that:
N = numel(A);
step = 2; % Set this to however many zeros you want to add each column
indx = N:-step:1;
M = numel(indx);
val = (1:N+step).' <= indx; % use bsxfun(@le, (1:N+step).',indx) instead for older MATLAB
val = repmat([A(:);zeros(step,1)],1,M).* val;
out = reshape(val(1:N*M),N,[]);
I've replaced N:-1:1
with N:-step:1
, this is the main change. I also needed to add step
zeros to A
, instead of only one (this is the [A(:);zeros(step,1)]
, where before it was [A(:);0]
). And I adjusted sizes everywhere to account for the smaller output array.
Note that this does not produce any of the empty (all-zero) columns. To add those, it is simplest to do:
out2 = zeros(N,N);
out2(:,1:size(out,2)) = out;