Logical indexing two dimensions. How do I avoid a nested for loop?
显示 更早的评论
I have a matrix X with doubles of size = 450 8156 and another matrix C with logical values with size = 64 8156 where 400 of the 8156 elements of each of the 64 rows are true. And I also have yet another matrix T of logical values with size = 9 450 where 400 of the 450 elements of each row are true.
I want to extract all the true values of X so that the resulting matrix becomes of size = 400 400 9 64
I can do this easily with a double for loop like this:
for n = 1 : 64
for z = 1 : 9
A(:,:,z,n) = X(T(z,:)',C(n,:));
end
end
but it is very slow. Is there a faster, more reasonable, vectorized way of doing thing? By using repmat, reshape, bsxfun, arrayfun etc.? Anyone good with logical indexing that can help me out?
Thanks.
5 个评论
Jos (10584)
2016-4-15
Did you pre-allocate A? Doing so, should make the for-loops pretty fast.
Peta
2016-4-15
Stephen23
2016-4-15
Your output array has 92160000 elements (92 million is nearly 1GB of double class) and it takes two seconds to process...
Generating intermediate variables is likely to take just as much time as the loops with a preallocated array, if not longer. Consider your time investment: are these loops really a bottleneck? Will the more complex code maintenance be worth it?
Peta
2016-4-15
采纳的回答
更多回答(2 个)
Azzi Abdelmalek
2016-4-15
You can improve your for loop by pre-allocating
A=zeros(400,400,9,64)
Jos (10584)
2016-4-15
Pre-allocate but also put the transpose out of the loop!
T = T.' ;
A = zeros(400,400,9,64)
for n = 1 : 64
for z = 1 : 9
A(:,:,z,n) = X(T(:,z), C(n,:));
end
end
Damn, this is fast :-)
2 个评论
Peta
2016-4-15
Jos (10584)
2016-4-17
tens of thousands of times x 0.1s per time (which is fast) = thousands of seconds = a few hours. Maybe Matlab is not the right tool for the job?
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!