Arrayfun - problem with input array

4 ビュー (過去 30 日間)
Damiano Capocci
Damiano Capocci 2018 年 2 月 26 日
回答済み: NISARGA G K 2018 年 3 月 5 日
I've found this kind of problem:
clear all;
a=[3,7,9,5,11,2];
b=[1,2,3,4,5,6];
l=[1,0,0,0,0,0];
f=@(x,z,y) x*l(y);
l=arrayfun(f,a,l,b);
which gives
l=[3,0,0,0,0,0]
on the contrary if i want to refere to "l" through the input value "z" in this way:
a=[3,7,9,5,11,2];
b=[1,2,3,4,5,6];
l=[1,0,0,0,0,0];
f=@(x,z,y) x*z(y);
l=arrayfun(f,a,l,b);
it gives
Index exceeds matrix dimensions.
Error in @(x,z,y)x*z(y)
why? is there a way of passing the array itself l=arrayfun(.....,l) and changing its value simultaneously?

採用された回答

NISARGA G K
NISARGA G K 2018 年 3 月 5 日
Hi Damiano,
I understand that you would like to change the value of array passed to 'arrayfun'.
B = arrayfun(func,A1,...,An) applies func to the elements of the arrays A1,...,An, so that B(i) = func(A1(i),...,An(i))
Since arrayfun access is element wise 'z' passed to function is scalar and hence z(y) is not possible. The following code would help you to change the value of array passed directly
a={[3,7,9,5,11,2]};
b={[1,2,3,4,5,6]};
l={[1,0,0,0,0,0]};
f=@(x,z,y) x.*z(y);
l=cellfun(f,a,l,b,'UniformOutput',0);

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by