重塑(或替代)数千个数据的有效方法 [英] Efficient way to reshape (alternately) thousands of data

查看:99
本文介绍了重塑(或替代)数千个数据的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的数据集,有数千行和数百列.我尝试为每第n行和所有第n行列数据交替重塑数据.我这样尝试过:

I have a data set which is very large, thousands of rows and hundreds of column. I try to alternately reshape the data for every nth row, and all the nth row column data. I tried like this:

in=rand(71760,320);
m=240; n=320;
[R,C]=size(in); 
out=[];
R_out=R/m; 

for k=1:m %from row 1 to mth row
    for i=1:C %reshape every column of mth row
        out=[out;reshape(in(k:m:end,i),R_out,1)'];
    end
end

如果您试用该代码,它花费了很长时间并且根本没有效率,您甚至都不会费心完成它.如何提高性能?还是有更好的方法呢?

If you try out the code, it took very long time and not efficient at all, you won't even bother to let it finish. How to increase the performance? Or there are better way to do it?

更新

此问题已扩展到另一个线程这里,以提高由@Teddy提供的重塑答案的性能

This question was extended to another thread here so as to improve the performance of reshaping answer provided by @Teddy

推荐答案

之所以需要很长时间,是因为out矩阵应为

The reason it takes so long is that the out matrix should be preallocated.

例如,此操作在我的笔记本电脑上大约在1秒钟内完成:

For example this completed in about 1 second on my laptop:

in=rand(71760,320);
m=240; n=320;
[R,C]=size(in); 
R_out=R/m; 

out=zeros(m*C,R_out);
for k=1:m %from row 1 to nth row
    for i=1:C %reshape every column of nth row
        out(i+C*(k-1),:) = in(k:m:end,i)';
    end
end

替代方法

最佳做法是使用使用arrayfun的矢量化方法,该方法可以像这样在一行中完成:

The best practice would be to use a vectorized approach using arrayfun which could be done in a single line like this:

out=cell2mat(arrayfun(@(k) in(k:m:end,:)', 1:m,'uniformoutput',0)');

这也可以更快地运行.

这篇关于重塑(或替代)数千个数据的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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