Fast method to append or prepend rows of zeros
4 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a 5001 by 762 matrix called posData. I would like to append and prepend, respectively, rows of zeros to posData. The number of rows appended and prepended to posData is given by a loop variable w, which ranges from 0 to 5001 in steps of 1.
For example, assuming that posData is a 5001 by 762 matrix, I have the following code:
tic
numColumns=size(posData,2);
for w=0:5001
firstM=[zeros(w,numColumns);posData];
secondM=[posData;zeros(w,numColumns)];
% perform operations on firstM and secondM
end
toc
Without doing any operations on the matrices firstM and secondM, the elapsed time on my computer is 713.175384 seconds. This seems like quite an expensive routine. Can you help me think if there is a faster way to do the above routine?
The other day, Walter pointed out to me that columns (and not rows) are contiguous in memory, so perhaps it would be faster to transpose my entire posData matrix and then prepend and append columns instead of rows. However, this will make the user input a bit more complicated. Is there any way to avoid transposing and yet still do the above routine more efficiently?
Thank you very much.
Andrew DeYoung
Carnegie Mellon University
1 个评论
bym
2011-5-22
you realize the first time through the loop you are appending & prepending with an empty matrix...
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!