Effacer les filtres
Effacer les filtres

The function "nchoosek" is not working in my code

6 vues (au cours des 30 derniers jours)
RDG
RDG le 13 Avr 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]

Réponses (1)

ChristianW
ChristianW le 13 Avr 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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by