Matlab - building a matrix by merging the same raw vector multiple times

前端 未结 2 1548
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 07:04

Is there a matlab function which allows me to do the following operation?

x = [1 2 2 3];

and then based on x I want to build the ma

2条回答
  •  星月不相逢
    2020-12-06 07:29

    You are looking for the REPMAT function:

    x = [1 2 2 3];
    m = repmat(x,4,1);
    

    You can also use indexing to repeat the rows:

    m = x(ones(4,1),:);
    

    or even outer-product:

    m = ones(4,1)*x;
    

    and also using BSXFUN:

    m = bsxfun(@times, x, ones(4,1))
    

提交回复
热议问题