setIO

PURPOSE ^

SETIO set input and output node of network by selectType.

SYNOPSIS ^

function [ network ] = setIO( network, inputNode, outputNode, selectType )

DESCRIPTION ^

 SETIO set input and output node of network by selectType.

    [ network ] = setIO( network, inputNode, outputNode, selectType )
    If inputNode & outputNode are lists of nodes, than setIO() will set the
    nodes in lists as input nodes and output nodes, respectively.
    If inputNode & outputNode are numbers, than how to choose nodes as input
    or output nodes is determined by selectType. If no selectType input
    argument exist, it use 'random' type. Below info is about selectType.
    random:
        randomly select nodes as input or output nodes.
    distant:
        select nodes at one side of ring as input, select nodes at the other
        side of ring as output, and the other nodes as inter nodes.

    Input:
        network: a network structure. When you want to use 'distant'
            in selectType, the network should be ring type small world network.
        inputNode: a number represent number of input nodes, and a vector
            represent id list of input nodes.
        outputNode: a number represent number of output nodes, and a vector
            represent id list of output nodes.
        selectType: the way to select nodes as input and output nodes.

    Output:
        network: a network structure with determined input and output nodes.

    Example:
        n = randomNetwork(9, 0.3, 0);
        randIONetwork = setIO(n, 4, 2, 'random');
        distantIONetwork = setIO(n, 2, 3, 'distant');

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [ network ] = setIO( network, inputNode, outputNode, selectType )
0002 % SETIO set input and output node of network by selectType.
0003 %
0004 %    [ network ] = setIO( network, inputNode, outputNode, selectType )
0005 %    If inputNode & outputNode are lists of nodes, than setIO() will set the
0006 %    nodes in lists as input nodes and output nodes, respectively.
0007 %    If inputNode & outputNode are numbers, than how to choose nodes as input
0008 %    or output nodes is determined by selectType. If no selectType input
0009 %    argument exist, it use 'random' type. Below info is about selectType.
0010 %    random:
0011 %        randomly select nodes as input or output nodes.
0012 %    distant:
0013 %        select nodes at one side of ring as input, select nodes at the other
0014 %        side of ring as output, and the other nodes as inter nodes.
0015 %
0016 %    Input:
0017 %        network: a network structure. When you want to use 'distant'
0018 %            in selectType, the network should be ring type small world network.
0019 %        inputNode: a number represent number of input nodes, and a vector
0020 %            represent id list of input nodes.
0021 %        outputNode: a number represent number of output nodes, and a vector
0022 %            represent id list of output nodes.
0023 %        selectType: the way to select nodes as input and output nodes.
0024 %
0025 %    Output:
0026 %        network: a network structure with determined input and output nodes.
0027 %
0028 %    Example:
0029 %        n = randomNetwork(9, 0.3, 0);
0030 %        randIONetwork = setIO(n, 4, 2, 'random');
0031 %        distantIONetwork = setIO(n, 2, 3, 'distant');
0032 %
0033 
0034 %   ---------
0035 %   Yen-Nan Lin, NTHU, 2010-2014, Matlab 2012a
0036 
0037 if nargin == 3
0038     selectType = 'random';
0039 end
0040 
0041 nNode = network.size;
0042 % if inputNode and outputNode is scalar
0043 if isscalar(inputNode) && isscalar(outputNode)
0044     nInput = inputNode;
0045     nOutput = outputNode;
0046     network.inputNumber = nInput;
0047     network.outputNumber = nOutput;
0048     switch selectType
0049         case 'random'
0050             ioList = randNoRepeat(nNode, nInput + nOutput);
0051         case 'distant'
0052             ioList = distantIOList(nNode, nInput, nOutput);
0053         otherwise
0054            error('Select type should be "random", "distant".');
0055     end
0056 % if inputNode and outputNode is vector
0057 elseif isvector(inputNode) && isvector(outputNode)
0058     nInput = length(inputNode);
0059     nOutput = length(outputNode);
0060     network.inputNumber = nInput;
0061     network.outputNumber = nOutput;
0062     % use find(..., 1) to prevent its size is (1, 1)
0063     dim = find( size(inputNode) ~= 1, 1);
0064     ioList = cat(dim, inputNode, outputNode);
0065 else
0066     error('inputNode & outputNode should be scalar or vector');
0067 end
0068 network.inputList = ioList(1:nInput);
0069 network.outputList = ioList((nInput + 1):end);
0070 network = sortByIO(network, ioList);
0071 end
0072 
0073 function [ randList ] = randNoRepeat( randMax, nRand )
0074 randList = randperm(randMax);
0075 randList = randList(1:nRand);
0076 end
0077 
0078 function [ ioList ] = distantIOList( nNode, nInput, nOutput )
0079 nInter = nNode - nInput - nOutput;
0080 inputList = 1:nInput;
0081 outputStartIx = nInput + floor(nInter / 2) + 1;
0082 outputList = outputStartIx:(outputStartIx + nOutput - 1);
0083 ioList = [inputList, outputList];
0084 end
0085 
0086 function [ network ] = sortByIO( network, ioList )
0087 for ix = 1:length(ioList)
0088     wantNode = ioList(ix);
0089     if isnumeric(ioList)
0090         wantNodeIndex = find(network.label == wantNode, 1);
0091     elseif ischar(ioList)
0092         error('Still cannot access string label');
0093     end
0094     network.matrix = swapCol(network.matrix, ix, wantNodeIndex);
0095     network.matrix = swapRow(network.matrix, ix, wantNodeIndex);
0096     network.label = swapCol(network.label, ix, wantNodeIndex);
0097 end
0098 end
0099 
0100 function [ matrix ] = swapCol( matrix, col1, col2 )
0101 matrix(:, [col1, col2]) = matrix(:, [col2, col1]);
0102 end
0103 
0104 function [ matrix ] = swapRow( matrix, col1, col2 )
0105 matrix([col1, col2], :) = matrix([col2, col1], :);
0106 end

Generated on Thu 30-Jan-2014 00:00:07 by m2html © 2005