Want to apply two different functions on different elements of a vector.

조회 수: 2(최근 30일)
Vinay Killamsetty
Vinay Killamsetty 2019년 5월 9일
댓글: Vinay Killamsetty 2019년 5월 9일
How can we operate two seperate functions on elements of a row vector: one function should be operated on even position elements and other function should be operated on odd position elements by using array function or by any other means.
Finally the outcomes of those operations should be combined as a another row vector
  댓글 수: 2
Vinay Killamsetty
Vinay Killamsetty 2019년 5월 9일
a=1
b=5
odd=@(w) a*w+b*w^2
even=@(w) a*w^2+b*w
curr_w=[some user defined 1*n vector];
I want to perform even function on even position elements in curr_w and odd function on odd position elements to get the output vector curr_w.
If you need any additional information please let me know

댓글을 달려면 로그인하십시오.

채택된 답변

Alex Mcaulley
Alex Mcaulley 2019년 5월 9일
편집: Alex Mcaulley 2019년 5월 9일
a = 1;
b = 5;
odd = @(w) a*w+b*w^2;
even = @(w) a*w^2+b*w;
curr_w = rand(1,5);
aa = arrayfun(odd,curr_w(1:2:numel(curr_w)));
bb = arrayfun(even,curr_w(2:2:numel(curr_w)));
res(1:2:numel(curr_w)) = aa;
res(2:2:numel(curr_w)) = bb;

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by