【发布时间】:2016-12-27 08:39:15
【问题描述】:
我想使用 Python 3 将 (2×2) 矩阵中的每个元素扩展为 (3×2) 块 --- 使用专业而优雅的代码。由于我不知道python代码,所以我将在数学中描述以下内容
X = # X is an 2-by-2 matrix.
1, 2
3, 4
d = (3,2) # d is the shape that each element in X should be expanded to.
Y = # Y is the result
1, 1, 2, 2
1, 1, 2, 2
1, 1, 2, 2
3, 3, 4, 4
3, 3, 4, 4
3, 3, 4, 4
并不是说现在 X 中的每个元素都是 Y 中的 3×2 块。块在 Y 中的位置与元素在 X 中的位置相同。
这是 MATLAB 代码
X = [1,2;3,4];
d = [3,2]
[row, column] = size(X);
a = num2cell(X);
b = cell(row, column);
[b{:}] = deal(ones(d));
Y = cell2mat(cellfun(@times,a,b,'UniformOutput',false));
感谢您的帮助。提前致谢。
【问题讨论】:
-
X 是
[[1,2],[3,4]]还是[[1,3],[2,4]]?
标签: matlab python-3.x numpy matrix