Method 1: Are you allowed to use ones
? Try this -
A = [1 2]
rowIdx = [1 : size(A,1)]';
colIdx = [1 : size(A,2)]';
out = A(rowIdx(:, ones(3,1)), colIdx(:, ones(4,1)))
Output
out =
1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2
Method 2: Are you allowed to use bsxfun
and permute
? Try this for the same result -
A = [1 2]
row_mapped = bsxfun(@plus,A,zeros(3,1))
out = reshape(bsxfun(@plus,row_mapped,permute(zeros(4,1),[3 2 1])),[3 8])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…