The function "nchoosek" is not working in my code

4 ビュー (過去 30 日間)
RDG
RDG 2013 年 4 月 13 日
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 Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by