matlab - Cell Array and function handle -


look @ below code:

for i=1:2 if == 1     f{i}= @(x) x(i)+x(i+1); else     f{i}= @(x) x(i-1)-x(i)+2; end end 

i wanted have stored in f f={@(x) x(1)+x(2);@(x) x(1)-x(2)+2;}. how should edit code achieve this? can me?

i think it's displaying issue, rather functional issue.

doing this:

for = 1:2     if == 1         f{i}= @(x) x(i)+x(i+1);     else         f{i}= @(x) x(i-1)-x(i)+2;     end end 

actually gives correct results:

>> f{1}([1 2 3 4]) ans =      3    % == x(1)+x(2), i==1  >> f{2}([1 2 3 4]) ans =      1    % == x(1)-x(2)+2, i==2 

but functions displayed "incorrectly":

>> f f =      @(x)x(i)+x(i+1)    @(x)x(i-1)-x(i)+2 

if want them displayed correctly well, you'll have messy:

for i=1:2      if == 1                 f{i} = str2func(['@(x)x(' num2str(i) ')+x(' num2str(i+1) ')']);     else                 f{i} = str2func(['@(x)x(' num2str(i-1) ')-x(' num2str(i) ')+2']);     end end 

results:

>> f f =      @(x)x(1)+x(2)    @(x)x(1)-x(2)+2 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -