犰狳相当于Matlab置换吗? [英] Armadillo equivalent of Matlab permute?

查看:70
本文介绍了犰狳相当于Matlab置换吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个arma::cube mycube(5,10,15);,我想对其尺寸进行置换,就像在matlab中所做的那样:

I have a arma::cube mycube(5,10,15); and I want to permute its dimensions, as one would do in matlab:

mycube = ones(5,10,15);
mycube = permute(mycube,[3 1 2]);
size(mycube) % returns (15 5 10)

有办法做到吗?
效率太低吗?

Is there a way to do that?
Would it be too inefficient?

我实际上是想进行3D FFT,所以我想到将第一个维度和第三个维度进行排列,以便能够使用arma::fft,然后向后进行排列.

I actually want to do a 3D FFT, so I thought of permuting the first and third dimensions to be able to use arma::fft, and then permuting back.

推荐答案

Armadillo库不包含此类功能,但是您可以实现简化版本.例如:

Armadillo ibrary does not contain such a function, but you can implement a simplified version. For example so:

#include <iostream>
#include <armadillo>
#include <tuple>
#include <algorithm>
#include <vector>

typedef std::tuple<std::size_t,std::size_t,std::size_t> D3tuple;

void printSize(const arma::cube &cube);
void simplePermute(arma::cube &cube, const D3tuple &order);
arma::uword getSize(const arma::cube &cube,
                    const std::size_t &n);
D3tuple get_coeff(arma::cube &cube, const D3tuple &order);

int main(int argc, char** argv)
  {

  arma::cube mycube = arma::randu<arma::cube>(2,2,2);
  std::cout<<mycube<<std::endl;
  printSize(mycube);

  simplePermute(mycube,D3tuple(3,1,2));
  printSize(mycube);
  std::cout<<mycube<<std::endl;
  return 0;
  }

void printSize(const arma::cube &cube)
    {
    std::cout<<cube.n_rows<<" "<<cube.n_cols<<" "<<cube.n_slices<<std::endl;
    }

void simplePermute(arma::cube &cube, const D3tuple &order)
    {
    auto first = std::get<0>(order),
        second = std::get<1>(order),
        third = std::get<2>(order);
    std::size_t cols = getSize(cube,first),
        rows = getSize(cube,second) ,
        slices = getSize(cube,third);

    arma::cube temp(cols,rows,slices);
    std::size_t c1,c2,c3;
    std::tie(c3,c2,c1) = get_coeff(cube,order);
    std::size_t index = 0;
    for(std::size_t i = 0;i<cols;i++)
        for(std::size_t j = 0;j<rows;j++)
            for(std::size_t k = 0;k<slices;k++)
                temp[index++] = cube[c1*i+c2*j+c3*k];

    cube = temp;
    }

arma::uword getSize(const arma::cube &cube,
                    const std::size_t &n)
    {
    switch (n)
        {
        case 1 : return cube.n_rows;
        case 2 : return cube.n_cols;
        case 3 : return cube.n_slices;
        }
    return 0;
    }

D3tuple get_coeff(arma::cube &cube, const D3tuple &order)
    {
    std::size_t c1,c2,c3;
    switch (std::get<0>(order))
        {
        case 1 : 
            c1 =  1;break;
        case 2 : 
            c1 =  cube.n_rows; break;
        case 3 : 
            c1 =  cube.n_rows*cube.n_cols; break;
        }
    switch (std::get<1>(order))
        {
        case 1 : 
            c2 =  1; break;
        case 2 : 
            c2 =  cube.n_rows; break;
        case 3 : 
            c2 =  cube.n_rows*cube.n_cols; break;
        }
    switch (std::get<2>(order))
        {
        case 1 : 
            c3 =  1; break;
        case 2 : 
            c3 =  cube.n_rows; break;
        case 3 : 
            c3 =  cube.n_rows*cube.n_cols; break;
        }
    return std::make_tuple(c1,c2,c3);
    }

这篇关于犰狳相当于Matlab置换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆