python数组与matlab(octave)数组之间的转换

classdef matpy
    %MATPY Summary of this class goes here
    %   Detailed explanation goes here
      properties
      end
      methods(Static)
          function result = mat2nparray( matarray )
              %mat2nparray Convert a Matlab array into an nparray
              %   Convert an n-dimensional Matlab array into an equivalent nparray  
              data_size=size(matarray);
              if length(data_size)==1
                  % 1-D vectors are trivial
                  result=py.numpy.array(matarray);
              elseif length(data_size)==2
                  % A transpose operation is required either in Matlab, or in Python due
                  % to the difference between row major and column major ordering
                  transpose=matarray';
                  % Pass the array to Python as a vector, and then reshape to the correct
                  % size
                  result=py.numpy.reshape(transpose(:)', int32(data_size));
              else
                  % For an n-dimensional array, transpose the first two dimensions to
                  % sort the storage ordering issue
                  transpose=permute(matarray,[length(data_size):-1:1]);
                  % Pass it to python, and then reshape to the python style of matrix
                  % sizing
                  result=py.numpy.reshape(transpose(:)', int32(fliplr(size(transpose))));
              end
          end
          function result = nparray2mat( nparray )
              %nparray2mat Convert an nparray from numpy to a Matlab array
              %   Convert an n-dimensional nparray into an equivalent Matlab array
              data_size = cellfun(@int64,cell(nparray.shape));
              if length(data_size)==1
                  % This is a simple operation
                  result=double(py.array.array('d', py.numpy.nditer(nparray)));
              elseif length(data_size)==2
                  % order='F' is used to get data in column-major order (as in Fortran
                  % 'F' and Matlab)
                  result=reshape(double(py.array.array('d', ...
                      py.numpy.nditer(nparray, pyargs('order', 'F')))), ...
                      data_size);
              else
                  % For multidimensional arrays more manipulation is required
                  % First recover in python order (C contiguous order)
                  result=double(py.array.array('d', ...
                      py.numpy.nditer(nparray, pyargs('order', 'C'))));
                  % Switch the order of the dimensions (as Python views this in the
                  % opposite order to Matlab) and reshape to the corresponding C-like
                  % array
                  result=reshape(result,fliplr(data_size));
                  % Now transpose rows and columns of the 2D sub-arrays to arrive at the
                  % correct Matlab structuring
                  result=permute(result,[length(data_size):-1:1]);
              end
          end
          function test()
              A = 1:5;
              Anp = matpy.mat2nparray(A);
              sa = size(A);
              sAnp = cellfun( @(x) double(x), cell(Anp.shape));
              assert (all(sAnp == sa));
              for i1=1:size(A,1)
                  for i2=1:size(A,2)
                      assert(A(i1,i2) == Anp.item(int32(i1-1), int32(i2-1)));
                  end
              end
              Anpm = matpy.nparray2mat(Anp);
              assert(all(A == Anpm));
              A = reshape(1:6, [2,3]);
              Anp = matpy.mat2nparray(A);
              sa = size(A);
              sAnp = cellfun( @(x) double(x), cell(Anp.shape));
              assert (all(sAnp == sa));
              for i1=1:size(A,1)
                  for i2=1:size(A,2)
                      assert(A(i1,i2) == Anp.item(int32(i1-1), int32(i2-1)));
                  end
              end
              Anpm = matpy.nparray2mat(Anp);
              assert(all(all(A == Anpm)));
              A = reshape(1:(2*3*4), [2,3,4]);
              Anp = matpy.mat2nparray(A);
              sa = size(A);
              sAnp = cellfun( @(x) double(x), cell(Anp.shape));
              assert (all(sAnp == sa));
              for i1=1:size(A,1)
                  for i2=1:size(A,2)
                      for i3=1:size(A,3)
                          display(sprintf('%d %d %d -> %f %f', i1,i2,i3, A(i1,i2,i3), Anp.item(int32(i1-1), int32(i2-1), int32(i3-1))));
                          assert (A(i1,i2,i3) == Anp.item(int32(i1-1), int32(i2-1), int32(i3-1)))
                      end
                  end
              end
              Anpm = matpy.nparray2mat(Anp);
              assert(all(all(all(A == Anpm))));
          end
      end
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fireAj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值