How do I change the increment in a loop

345 次查看(过去 30 天)
I have been asked to write a function that calculates the sum of the series;
2^(5*i-1)
where i = 1,3,5... I currently have
function s = summation(N)
% Syntax
% s = summation (N)
% Input
% N = Number of terms in series
%Output
% sum of series
%Initilising loop to be zero
s = 0;
for i = 1:N
% loop adds previous value of s to the next one
s = s + 2^(5*i-1);
end
% increasing increment i by 2 from 1 [1,3,5...etc]
i = i+2;
end
which calculates the sum for i=1,2,3.... How do I change the increment of i?

回答(2 个)

Stephen23
Stephen23 2017-5-9
编辑:Stephen23 2017-5-9
i = 1:2:N
^ define the step size here!
The colon operator is clearly explained in the documentation:
  2 个评论
Jan
Jan 2017-5-9
@MATTHEW FOX: See also: doc for
Note: Whenever you have questions concerning a specific command, read the documentation at first. Matlab's docs are the best I've ever read. They are useful and clear, and the "See also:" lines are smart guesses of what the user might be interested also in, when the command does not perfectly solve the problem.
Noman Rathore
Noman Rathore 2018-12-5
Hello i am New researcher , and new to Matlab programming , but i understand the basius of programming , my querry is how i can use help and support for guidance for programming my own program. i most of the time do not find the useful help.
Nomi

请先登录,再进行评论。


Phirime Monyeki
Phirime Monyeki 2020-2-10
N = 100 ; % should be multiple of the number of parts you want
th = linspace(0,2*pi) ;
y = cos(th) ;
plot(th,y)
% divide the section into 5 equal parts
th1 = reshape(th,5,[]) ;
y1 = reshape(y,5,[])
  3 个评论
Walter Roberson
Walter Roberson 2023-3-12
help colon
: Colon. J:K is the same as [J, J+1, ..., J+m], where m = fix(K-J). In the case where both J and K are integers, this is simply [J, J+1, ..., K]. This syntax returns an empty matrix if J > K. J:I:K is the same as [J, J+I, ..., J+m*I], where m = fix((K-J)/I). This syntax returns an empty matrix when I == 0, I > 0 and J > K, or I < 0 and J < K. COLON(J,K) is the same as J:K and COLON(J,I,K) is the same as J:I:K. The colon notation can be used to pick out selected rows, columns and elements of vectors, matrices, and arrays. A(:) is all the elements of A, regarded as a single column. On the left side of an assignment statement, A(:) fills A, preserving its shape from before. A(:,J) is the J-th column of A. A(J:K) is [A(J),A(J+1),...,A(K)]. A(:,J:K) is [A(:,J),A(:,J+1),...,A(:,K)] and so on. The colon notation can be used with a cell array to produce a comma- separated list. C{:} is the same as C{1},C{2},...,C{end}. The comma separated list syntax is valid inside () for function calls, [] for concatenation and function return arguments, and inside {} to produce a cell array. Expressions such as S(:).name produce the comma separated list S(1).name,S(2).name,...,S(end).name for the structure S. For the use of the colon in the FOR statement, See FOR. For the use of the colon in a comma separated list, See VARARGIN. Documentation for colon doc colon Other uses of colon codistributed/colon duration/colon codistributor1d/colon embedded.fi/colon codistributor2dbc/colon gpuArray/colon datetime/colon matlab/colon deep.internal.recording.RecordingArray/colon sym/colon distributed/colon symbolic/colon dlarray/colon
Steven Lord
Steven Lord 2023-3-12
For the original question looking at the documentation for the colon function (which is the function name for the : operator) is correct. That's the tool you use to create a vector if you know the two desired endpoints and the increment.
For the answer under which these comments lie, you'd want to look at the documentation for the linspace function. That's the tool you use to create a vector if you know the two endpoints and how many elements you want the vector to have.
If you know one of the endpoints, the increment, and the number of elements you want the vector to have you can do that with colon as well.
startPoint = 1;
increment = 1;
numPoints = 10;
x = startPoint + increment*(0:numPoints-1)
x = 1×10
1 2 3 4 5 6 7 8 9 10
Keep an eye out for fencepost error in this last case.

请先登录,再进行评论。

类别

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