11 Ağustos 2008 Pazartesi

intpart.m

%    Author      : Senol Korkmaz < senol.korkmaz@gmail.com >
% Filename : intpart.m
% Requires : partitions.m < http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=12009&objectType=file >
% Description : A Matlab Function to calculate partitions of an integer
% Usage : intpart(n,m,k)
%
% Input : n is an integer to find out its partitions
% : m is a vector which indicates that how many numbers can be used
% : k is a vector which indicates that which numbers can be used
%
% Output : This function returns a matrice that includes partitions of n
%
% Version : 1.0
%
% License : GNU/GPL v3
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.

function iparts = intpart(n,m,k) % Declare function and its arguments

if (nargin==1) % There is only one input (n)
m = 1:n; % Consider that m is a vector that contains all integers from 1 to n
k = m; % Consider that k is a vector that contains all integers from 1 to n
end

if (nargin==2) % There are two inputs ( n & m)
k = 1:n; % Consider that k is a vector that contains all integers from 1 to n
end

partlist = partitions(n,k); % Calculate the partitions of n with the numbers whis is in the vector k

% Code explanations will be written as soon as possible

partlist = partlist(find(ismember(sum(partlist,2),m)==1),:);

iparts = zeros(size(partlist,1),max(m));

for i = 1:size(partlist,1)

tmpROW = [];

for j = 1 : size(partlist,2)
tmpROW = horzcat(tmpROW,repmat(k(j),1,partlist(i,j)));
end

tmpROW = horzcat(tmpROW,repmat(0,1,size(iparts,2)-size(tmpROW,2)));
iparts(i,:) = sort(tmpROW,'descend') ;
end

Hiç yorum yok:

Konular

Matematik (5) Kod (4) Gündem (2) Bilgisayar (1) İnternet (1)