Mathematica编程:Reshape函数

Mathematica具有强大的符号运算功能,可以实现复杂的数学符号运算和数值运算

1)编制Reshape 函数实现以下功能:假设A = { {1, 2}, {3, 4}, {5, 6}},利用 Reshape 函数 B = Reshape[A, {2, 3}],输出:B = { {1, 2, 3}, {4, 5, 6}}

A = {
    
    {
    
    1, 2}, {
    
    3, 4}, {
    
    5, 6}};
B = ArrayReshape[A, {
    
    2, 3}]
{
    
    {
    
    1, 2, 3}, {
    
    4, 5, 6}}
Reshape[A_, size_] := (
  temp = {
    
    }; rows = size[[1]]; cols = size[[2]];
  Do [temp = Join[temp, A[[i]]], {
    
    i, 1, Length[A]}]; 
  Table[temp[[i*cols + j]], {
    
    i, 0, rows - 1}, {
    
    j, 1, cols}])
Reshape[A, {
    
    2, 3}]

猜你喜欢

转载自blog.csdn.net/runrundream/article/details/127234600