필터 지우기
필터 지우기

simple coding, how to write (x-1)...(x-n)

조회 수: 1 (최근 30일)
ahmed lamak
ahmed lamak 2016년 9월 5일
댓글: Stephen23 2016년 9월 5일
How would i write (x-1)(x-2)....(x-n)
for a given n in matlab

답변 (2개)

Image Analyst
Image Analyst 2016년 9월 5일
Try this:
result = 1
for k = 1 : n
result = result * (x - k);
end
  댓글 수: 2
ahmed lamak
ahmed lamak 2016년 9월 5일
i gave a simple example as the zeros being 1 : n, if they were rather complicated values and denoted as say z(1),z(2),...,z(n) this method wont work?
Image Analyst
Image Analyst 2016년 9월 5일
You can do this:
result = 1
for k = 1 : length(z)
result = result * (x - z(k));
end

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


Stephen23
Stephen23 2016년 9월 5일
편집: Stephen23 2016년 9월 5일
The simplest solution, without any loops:
prod(x-z)
  댓글 수: 2
Walter Roberson
Walter Roberson 2016년 9월 5일
I do not understand why you are raising to the z'th power ??
Stephen23
Stephen23 2016년 9월 5일
@Walter Roberson: experimenting around, and not paying enough attention to the copy-and-paste :(

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by