nchoosek with range of k values and ordered sequential value sets
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
The below:
function cnkMatAll = NChooseKRange(nVect, kRange)
% NChooseKRange
%
% Combinations of N taken as k = C of 3 taken as 2 (N = {1,2,3}, k = 2)
%
% NChooseKRange(1:3,2)
%
% ans =
%
% 1 2 % k = 2
% 1 3 % k = 2
% 2 3 % k = 2
% nVect has a range of values: it defines the elements of the N Set
%
% kRange CAN equally a range of k values (not mandatory). If k = 1:2 then:
% NChooseKRange(1:3, 1:2)
%
% ans =
%
% 1 0 % k = 1
% 2 0 % k = 1
% 3 0 % k = 1
% 1 2 % k = 2
% 1 3 % k = 2
% 2 3 % k = 2
lastVal = kRange(end);
parfor i = kRange
cnk = nchoosek(nVect, i);
cnk = padarray(cnk,[0,lastVal-i],0,'post');
cnkMat(i) = {cnk};
end
elRowSizeV = zeros(1,numel(cnkMat));
parfor nr = 1:numel(cnkMat)
el = cnkMat(nr);
elRowSizeV(nr) = size(el{1},1);
end
cumRowSize = cumsum(elRowSizeV);
cnkMatAll = zeros(cumRowSize(end), kRange(end));
for nr = 1:numel(cnkMat)
el = cnkMat(nr);
if nr == 1
cnkMatAll(1:cumRowSize(nr),:) = el{1};
else
cnkMatAll(cumRowSize(nr-1)+1:cumRowSize(nr),:) = el{1};
end
end
end
is an extension of nchoosek as can be seen. Two questions:
1) Is there a better way to obtain this and the more important one is:
2) Can nchoosek be modified such that it returns only "contiguous" value ranges ... such that line:
NChooseKRange(1:3, 1:2) => 1 3 % k = 2
does NOT get returned at all?
Or is there another that would return only contiguous values of the set N (being a subset of what nchoosek currently returns)?
Thanks!
1 comentario
Guillaume
el 10 de Ag. de 2018
Editada: Guillaume
el 10 de Ag. de 2018
Is there a better way to obtain this
I find this function badly designed. What if nVect contains 0?
In my opinion, merging and padding the sets for each k is wrong, keep each one separate in a cell array:
function cnkMatAll = NChooseKRange(nVect, kRange)
%cnkMatAll: a cell array the same size as kRange
cnkMatAll = arrayfun(@(k) nchoosek(nVect, k), kRange, 'UniformOutput', false);
end
Can nchoosek be modified such that it returns only "contiguous" value ranges
It sounds like what you want is nothing like nchoosek. In particular, it sounds like your nVect input has a particular form. Otherwise, what would be the output for nVect = 1:3:12?
If all you want is vectors of continuous values of a given length then there are much faster way to generate this.
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!