Create a vector x with the elements

조회 수: 30 (최근 30일)
Noorhafizah
Noorhafizah 2011년 2월 3일
편집: SIBUNABIRWA 2023년 11월 30일
Create a vector x with the elements x = (-1^n + 1/2*n -1 ) Add up the elements of the version of this vector that has 100 elements.
Can someone tell me how to do it?
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 2월 3일
Is that (-1)^n ? Does the 1/2 apply to n or to (n-1) ? Is n a constant, or does it represent the index of the element? If it represents the index, should n start at 0 or at 1 ?
the cyclist
the cyclist 2011년 2월 3일
If this is class homework, please say so and describe what you have tried on your own.

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

답변 (2개)

Davide Ferraro
Davide Ferraro 2011년 2월 3일
You can use vector operations on the vector n with elements from 1 to 100:
x = (-1.^n + 1/2*n -1);
to create the x vector.
If you need to sum every element you can then use the SUM command:
sum(x)

Barnali
Barnali 2023년 8월 21일
편집: DGM 2023년 10월 16일
n = 1:100;
x = ((-1).^(n+1))./(2*n-1);
sum(x)
ans = 0.7829
  댓글 수: 1
SIBUNABIRWA
SIBUNABIRWA 2023년 11월 30일
편집: SIBUNABIRWA 2023년 11월 30일
It is working out. Thank you very much

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by