I am looking for a โgoodโ way to find a matrix (figure) in a larger matrix (an arbitrary number of dimensions).
Example:
total = rand(3,4,5); sub = total(2:3,1:3,3:4);
Now I want this to happen:
loc = matrixFind(total, sub)
In this case, loc
should become [2 1 3]
.
For now, I'm just interested in finding one point (if it exists), and I'm not worried about rounding issues. It can be assumed that sub
"fits" in total
.
Here is how I could do it for 3 dimensions, however it just seems like the best way:
total = rand(3,4,5); sub = total(2:3,1:3,3:4); loc = []; for x = 1:size(total,1)-size(sub,1)+1 for y = 1:size(total,2)-size(sub,2)+1 for z = 1:size(total,3)-size(sub,3)+1 block = total(x:x+size(sub,1)-1,y:y+size(sub,2)-1,z:z+size(sub,3)-1); if isequal(sub,block) loc = [xyz] end end end end
I hope to find a workable solution for an arbitrary number of measurements.