16 Ağustos 2008 Cumartesi

bolumle.m

%    Yayıncı     : Şenol Korkmaz < senol.korkmaz@gmail.com >
% Dosya Adı : bolumle.m
% Gerekenler : partitions.m < http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=12009&objectType=file >
% Tanım : Sayı Bölümleme maksatlı MATLAB işlevi
% Kullanım : bolumle(n,m,k)
%
% Girdi : n --> Bölümlenecek sayı
% : m --> Bölümlemelerin kaçar sayıdan oluşacağını belirten vektör
% : k --> Bölümlemede kullanılacak sayıların seçileceği vektör
%
% Çıktı : Bu işlev bölümlemeleri döndürür
%
% Sürüm : 1.0
%
% Lisans : GNU/GPL v3 <http://www.gnu.org/licenses/>

function bolumler = bolumle(n,m,k) % İşlevi ve girdilerini tanımla

if (nargin==1) % Eğer sadece bir girdi var (n) ise
m = 1:n; % m = 1 den n'ye kadar tamsayıları içeren vektör
k = m; % k = 1 den n'ye kadar tamsayıları içeren vektör
end

if (nargin==2) % Eğer sadece iki girdi var (n & m) ise
k = 1:n; % k = 1 den n'ye kadar tamsayıları içeren vektör
end

% k vektöründeki sayıları kullanarak harici partitions fonksiyonu ile n'i bölümle
% hamBolumler, her bölümlemede hangi sayının kaçar defa kullanıldığı bilgisini içerecek
hamBolumler = partitions(n,k);

hamBolumler = hamBolumler(find(ismember(sum(hamBolumler,2),m)==1),:); % m vektöründe belirtilenler kadar sayı kullanılmış bölümlemeleri al

bolumler = zeros(size(hamBolumler,1),max(m)); % Çıktı şablonu

for i = 1:size(hamBolumler,1) % Her bir ham bölüm için döngü

buSatir = []; % O an işlenecek satır için boş vektör

for j = 1 : size(hamBolumler,2) % Her bir ham bölümü oluşturan her bir sayı için döngü

% hamBolumler vektöründe her sayının kaçar defa kullanıldığı bilgisi yer almaktadır
% bu bilgiyi kullanarak buSatır vektörüne o sayıları kullanıldıkları kadar ( repmat ile)
% ekler ve onları azalan sıra ile sıralarız
buSatir = horzcat(buSatir,repmat(k(j),1,hamBolumler(i,j)));
end

% buSatır vektörünün boyu max(m) kadar olmalıdır, bu yüzden kalan yerlere 0 doldurulur
buSatir = horzcat(buSatir,repmat(0,1,size(bolumler,2)-size(buSatir,2)));

% buSatır vektörünü çıktıya ekle
bolumler(size(hamBolumler,1)-i+1,:) = sort(buSatir,'descend') ;
end

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

Konular

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