Efficient way to reshape (alternately) thousands of data

▼魔方 西西 提交于 2019-12-13 07:24:51

问题


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?

UPDATE

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


回答1:


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

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

Alternative method

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)');

this also runs more quickly.



来源:https://stackoverflow.com/questions/42943598/efficient-way-to-reshape-alternately-thousands-of-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!