Using MatLab, I have a 2D matrix which I want to sample from. The position are known and fixed every time.
A nested For loop is slow. Currently I am using logical indexing e.g.
cords = [ 1 0 1; 0 0 0 ]; cords = logical(cords)
data = aMatrix(cords);
My main question is: is there an even faster method than this?
Another query I have, by using logical indexing, does that sample that data in the same order every time, this is important.
Logical indexing is generally faster than linear indexing or subscripting, probably on account of the usual call to find
. If you have the logical matrix, use it. If you have have linear indexes, use them. Don't try to convert just to speed up the actual indexing step.
The order of the output data
obtained from logical indexing will be in the same order as the indexes returned by find(cords)
(linear indexing).