Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 在Matlab中得到每个子矩阵n列作为新矩阵中的列_Performance_Matlab_Matrix - Fatal编程技术网

Performance 在Matlab中得到每个子矩阵n列作为新矩阵中的列

Performance 在Matlab中得到每个子矩阵n列作为新矩阵中的列,performance,matlab,matrix,Performance,Matlab,Matrix,我处理的是非常大的数据集(数十万列,14行),我需要将每个子矩阵n列作为新矩阵中的一列,即 1 3 5 7 2 4 6 8 变成 1 3 5 2 4 6 3 5 7 4 6 8 当n=2时 我现在拥有的是 n_data_points = size(data1, 1); small_n = 60; big_n = size(data1, 2); new_2 = bsxfun(@(x,y)(data1(x + n_data_points * (y - 1))), (1:(n_data_poin

我处理的是非常大的数据集(数十万列,14行),我需要将每个子矩阵n列作为新矩阵中的一列,即

1 3 5 7
2 4 6 8
变成

1 3 5
2 4 6
3 5 7
4 6 8
当n=2时

我现在拥有的是

n_data_points = size(data1, 1);
small_n = 60;
big_n = size(data1, 2);

new_2 = bsxfun(@(x,y)(data1(x + n_data_points * (y - 1))), (1:(n_data_points * small_n)).', 1:(big_n - small_n + 1));
但这种方法相当缓慢。如何使用本机Matlab操作实现这一点

编辑

因此,在对这里的一些方法进行基准测试并进行更多研究后,我确定了以下几点:

n = 60;
[m, big_n] = size(data1);
a = zeros((m*n), (big_n - n + 1));
for i = 1:(big_n - n + 1)
    a(:, i) = reshape(data1(:, i:(i + n - 1)), 1, m*n);
end 
使用14×387160的矩阵,此方法大约需要2.3秒,而我的原始方法大约需要4.8秒,@Divakar的大约需要3.9秒

一种使用-

样本运行-

data1 =
     9     2     8     2     4     9     4
     9     3     3     3     8     3     6
     5     8     9     6     6     7     1
     2     3     4     5     5     7     1
n =
     3
out =
     9     2     8     2     4
     9     3     3     3     8
     5     8     9     6     6
     2     3     4     5     5
     2     8     2     4     9
     3     3     3     8     3
     8     9     6     6     7
     3     4     5     5     7
     8     2     4     9     4
     3     3     8     3     6
     9     6     6     7     1
     4     5     5     7     1

对于n=2,它非常简单:

new_data=[data1(:,1:end-1); data1(:,2:end)];
对于n>2,您可以循环它(我相信即使很快通过
circshift
,或
kron
等也可以完成):

例如:

n=3;
data1 = randi(10,2,6)

 7     8     5     5     1     1
 8     2     9     2     7     4



new_data =

 7     8     5     5
 8     2     9     2
 8     5     5     1
 2     9     2     7
 5     5     1     1
 9     2     7     4

为了完整起见,您忘记定义
n
。。。这似乎是一个快速的解决方案,但…@kkuilla问题将
n
作为一个参数。与问题中作为输入数组列出的数据1相同。是的,
bsxfun
内置:@plus似乎是解决这个问题的一个不错的选择。无耻的插件:在
R
语言中,这种操作更简单、更快。如果你要做大量的数据工作,你可能会考虑切换语言。哦,相信我,我很乐意。我是做研究的本科生,这不取决于我:PYes、circshift或toeplitz,我猜其中一个或那些也可以在这里使用!对于较大的n值,此方法比我的方法和@Divakar的方法慢得多。也许你可以用circshift或toeplitz或kron来举例说明这种潜力。
new_data=data1(:,1:end-n+1);
for k=2:n
  new_data=[new_data; data1(:,k:end-n+k)];
end
n=3;
data1 = randi(10,2,6)

 7     8     5     5     1     1
 8     2     9     2     7     4



new_data =

 7     8     5     5
 8     2     9     2
 8     5     5     1
 2     9     2     7
 5     5     1     1
 9     2     7     4