spfun not working as expected with an anonymous function

1 回表示 (過去 30 日間)
Kurt Ehlert
Kurt Ehlert 2018 年 8 月 3 日
編集済み: Kurt Ehlert 2018 年 8 月 3 日
I'm trying to run the following
spfun((@(n) nchoosek(n, 2)), 4*speye(4,4))
I get the following error
Error using sparse
Vectors must be the same lengths.
Error in spfun (line 22)
f = sparse(i,j,feval(fun,x),m,n);
I expect to see a 4x4 matrix with sixes on the diagonal. What am I doing wrong?
EDIT I came up with a somewhat inelegant fix:
spfun((@(n_vec) arrayfun(@(n) nchoosek(n, 2), n_vec)), 4*speye(4,4))

回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by