How to create this matrix in MATLAB -
i have vector such
a=[4;3;1;6]
and want create matrix elements below a
b=[6 5 4 3 2 1;4 3 2 1 0 0;3 2 1 0 0 0;1 0 0 0 0 0];
how can in matlab ? number of columns equal max of a.
here 2 ways this: 1 vectorized, , 1 in loop.
a=[4;3;1;6]; b = max(bsxfun(@minus, sort(a, 'descend'), 0:(max(a)-1)), 0);
or
s = sort(a, 'descend'); m = numel(a); n = s(1); c = zeros(m,n); k = 1:m c(k,1:s(k)) = s(k):-1:1; end
results:
b = 6 5 4 3 2 1 4 3 2 1 0 0 3 2 1 0 0 0 1 0 0 0 0 0
Comments
Post a Comment