Kronecker product with varying dimension in Matlab?

时间:2015-07-08 15:53:31

标签: matlab

I have a vector in Matlab A of dimension mx1 reporting the natural integers from 1 to m in increasing order, e.g. A=[1 2 3]'.

Let B be a vector of dimension mx1 reporting some natural integers greater or equal than zero, e.g. B=[1 3 0]'.

Without using loops, I want to construct a vector C of dimension sum_i(B(i)) obtained by listing each A(i) B(i) times. In the example C=[1 2 2 2].

1 个答案:

答案 0 :(得分:1)

您可以使用arrayfun

执行此操作
A = [1 2 3]';
B = [1 3 0]';
m = 3;
C = cell2mat(arrayfun(@(x) ones(B(x),1)*A(x), 1:m, 'UniformOutput', 0)');

C =

     1
     2
     2
     2