poster
2019-12-10, 16:49
假设我有一个1×12矩阵,我想将其大小调整为4×3矩阵。我该怎么办?
我当前的解决方案有点丑陋:
for n = 1:(length(mat)/3) out(n,1:3) = mat( ((n-1)*3 + 1):((n-1)*3 + 3) ); end 有一个更好的方法吗?
回答:
reshape当然是正确的解决方案,如@gnovice所述 (https://stackoverflow.com/a/793581/5211833) 。
reshape一个不错的功能是它允许:
A = 1:12; B = reshape(A,4,[]); B = 1 5 9 2 6 10 3 7 11 4 8 12 因此,如果您不知道会有多少列,那么reshape会为您计算出来。同样,如果您不进行修改,则reshape将填充行数。
C = reshape(A,[],4) C = 1 4 7 10 2 5 8 11 3 6 9 12
更多&回答... (https://stackoverflow.com/questions/793574)
我当前的解决方案有点丑陋:
for n = 1:(length(mat)/3) out(n,1:3) = mat( ((n-1)*3 + 1):((n-1)*3 + 3) ); end 有一个更好的方法吗?
回答:
reshape当然是正确的解决方案,如@gnovice所述 (https://stackoverflow.com/a/793581/5211833) 。
reshape一个不错的功能是它允许:
A = 1:12; B = reshape(A,4,[]); B = 1 5 9 2 6 10 3 7 11 4 8 12 因此,如果您不知道会有多少列,那么reshape会为您计算出来。同样,如果您不进行修改,则reshape将填充行数。
C = reshape(A,[],4) C = 1 4 7 10 2 5 8 11 3 6 9 12
更多&回答... (https://stackoverflow.com/questions/793574)