h =2;
P = [1, 4, 5, 7];
a1 = (1-h)* 1 + h * P(:, 1);
a2 = (1-h) * a1 + h * P(:, 2);
a3 = (1-h) * a2 + h * P(:, 3);
a4 = (1-h) * a3 + h * P(:, 4);
disp(a1) = 1
disp(a2) = 7
disp(a3) = 3
disp(a4) = 11

댓글 수: 2

Tino
Tino 2019년 5월 1일
Please I will appreciate the answer to this question
thanks in advance
Guillaume
Guillaume 2019년 5월 1일
To what question? You haven't asked anything, just given us some code with no explanation nor context.

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

 채택된 답변

madhan ravi
madhan ravi 2019년 5월 1일
편집: madhan ravi 2019년 5월 1일

0 개 추천

a = num2cell( (1-h) * repmat( (1-h) * 1 + h * P(1), 1, 4) + h * P ) ;
[a1, a2, a3, a4] = deal( a{:} )

댓글 수: 6

Note that deal is not needed in the above.
However, never create numbered variables. It's pointless, it makes the code more complex and has no advantages. Your original answer, which created a a vector was much better.
Another way of creating the a vector is with the filter function:
h = 2;
P = [1, 4, 5, 7];
a = filter(h, [1, h-1], P, -1)
madhan ravi
madhan ravi 2019년 5월 1일
Yes true and thank you for showing an additional method.
Tino
Tino 2019년 5월 1일
Hi Guillaume thanks for your swift response but if you check the code it does not follow sequence with different h values for instance
when h = 1
1
2
3
4
when h =2
1
3
3
5
when h =3
1
4
1
10
You would have to adjust the 4th input of filter to match the starting a(0) of 1. Another option with filter:
a = filter(h, [1, h-1], [1/h, P]); %calculates a(0), ..., a(numel(P)), with a(0) forced to 1
a = a(2:end); %keep a(1), ..., a(numel(P))
I don't get why when h = 1, you'd get 1 2 3 4, since in that case, a(i) = P(i) and P is [1 4 5 7]
Tino
Tino 2019년 5월 1일
편집: Tino 2019년 5월 1일
sorry the right figure is the one below. sorry for giving wrong computation
Can you help me with the short code to gives the following answers for h = any number for instance
when h = 1
1
4
5
7
when h = 2
1
7
7
3
when h = 3
1
10
1
13
thanks in advance
Guillaume
Guillaume 2019년 5월 1일
In your question, you told us than when h = 2, a = [1 7 3 11] (for P = [1, 4, 5, 7]) and this is the answer that my code produces. I have no idea how you get your new answer. It doesn't match the formula you've posted.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2019년 5월 1일

댓글:

2019년 5월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by