www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/@guidarray/forceGroupArray.m

    function obj = forceGroupArray(obj, array, groups)
%FORCEGROUPARRAY Return a GUIDARRAY that has the desired groups
%
%  G = FORCEGROUPARRAY(G, ARRAY, GROUPS)

%  Copyright 2000-2006 The MathWorks, Inc. and Ford Global Technologies, Inc.


% First check that the groups sum to the size of the guidarray
if sum(groups) ~= length(obj.values)
    error(message('mbc:guidarray:InvalidArgument', sum( groups ), length( obj.values )));
end

if length(array.values) ~= numel(groups)
    error(message('mbc:guidarray:InvalidArgument1', numel( groups ), length( array.values )));
end

if any(groups == 0)
    error(message('mbc:guidarray:InvalidArgument2'));
end

% Make sure that groups is vertical vector so we can concatenate with it
groups = groups(:);

% Work out the start index of each group from their sizes
groupStartIndex = cumsum([1 ; double(groups(1:end-1))]);

% If groups isn't an uint32 then convert
if ~isa(groups, 'uint32')
    groups = uint32(groups);
end

% To force a group guid onto an array we use the following properties. A
% group guid is the XOR (^) of the individual guids in that group. Thus
%
% groupGuid = r1 ^ r2 ^ r3 ^ ... ^ rN.
% 
% If we are allowed to modify r1 then to get a desired groupGuid we write
%
% newr1 = desiredGroupGuid ^ r2 ^ r3 ^ ... ^ rN
%
% Which gives desiredGroupGuid = newr1 ^ r2 ^ r3 ^ ... ^ rN
% 
% So we replace r1 with the desired groupGuid, calculate the group guids
% and then replace r1 with that result we have force the request group
% guids.

% Replace the first element in each group with the requested group guid to
% deduce the new first element;
obj.values(groupStartIndex) = array.values;

% Calculate that group guid
array = getGroupArray(obj, groups);

% And put the values back in
obj.values(groupStartIndex) = array.values;

% Update the hash
obj = updateHash(obj);