The function "nchoosek" is not working in my code

6 次查看(过去 30 天)
I'm currently having a problem. Suppose I've a code as such:
%2 cell arrays named cnt with contents as follow:%
cnt{1}=[3;1;3;2;1;2]
cnt{2}=[2;3;1;3;1;2]
%Finds the index location for values not 1
for i=1:2
ind=find(cnt{i}~=1)
end
%Performs nchoosek for the values corresponding to the indices
for i=1:2
for j=1:size(cnt{i},1)
value{i}=nchoosek(cnt{i}(ind(j)),2)
end
end
The error returned is "??? Error using ==> nchoosek at 50 K must be an integer between 0 and N.
Error in ==> Workshop at 118 value{i}=nchoosek(cnt{i}(ind(j)),2)"
Is there a more direct way besides for loop? [Since it's not efficient and is not returning my desired result]

回答(1 个)

ChristianW
ChristianW 2013-4-13
cnt{1}=[3;1;3;2;1;2]
cnt{2}=[2;3;1;3;1;2]
value = cellfun(@(x) nchoosek(x(x~=1),2),cnt,'un',0);
or with loop
for i = 1:length(cnt)
value{i} = nchoosek(cnt{i}(cnt{i}~=1),2);
end

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by