필터 지우기
필터 지우기

The function "nchoosek" is not working in my code

조회 수: 6 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by