The function "nchoosek" is not working in my code

4 Ansichten (letzte 30 Tage)
RDG
RDG am 13 Apr. 2013
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]

Antworten (1)

ChristianW
ChristianW am 13 Apr. 2013
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

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by