General method to find submatrix in matlab matrix

前端 未结 3 2022
臣服心动
臣服心动 2020-11-29 08:50

I am looking for a \'good\' way to find a matrix (pattern) in a larger matrix (arbitrary number of dimensions).

Example:

total = rand(3,4,5);
sub = t         


        
3条回答
  •  长情又很酷
    2020-11-29 09:30

    Here is low-performance, but (supposedly) arbitrary dimensional function. It uses find to create a list of (linear) indices of potential matching positions in total and then just checks if the appropriately sized subblock of total matches sub.

    function loc = matrixFind(total, sub)
    %matrixFind find position of array in another array
    
        % initialize result
        loc = [];
    
        % pre-check: do all elements of sub exist in total?
        elements_in_both = intersect(sub(:), total(:));
        if numel(elements_in_both) < numel(unique(sub))
            % if not, return nothing
            return
        end
    
        % select a pivot element
        % Improvement: use least common element in total for less iterations
        pivot_element = sub(1);
    
        % determine linear index of all occurences of pivot_elemnent in total
        starting_positions = find(total == pivot_element);
    
        % prepare cell arrays for variable length subscript vectors
        [subscripts, subscript_ranges] = deal(cell([1, ndims(total)]));
    
    
        for k = 1:length(starting_positions)
            % fill subscript vector for starting position
            [subscripts{:}] = ind2sub(size(total), starting_positions(k));
    
            % add offsets according to size of sub per dimension
            for m = 1:length(subscripts)
                subscript_ranges{m} = subscripts{m}:subscripts{m} + size(sub, m) - 1;
            end
    
            % is subblock of total equal to sub
            if isequal(total(subscript_ranges{:}), sub)
                loc = [loc; cell2mat(subscripts)]; %#ok
            end
        end
    end
    

提交回复
热议问题