decimal increment inside a for loop

Hi, I am trying to run a for loop as part of my code. I am trying to have 15 j values between z2 and z1 which are decimal numbers and change with every iteration. The error I am getting is "Subscript indices must either be real positive integers or logicals."I know I am getting the increments wrong but need some suggestions to solve the error.
for i = 1:1:49
X_even = [];
C_even = [];
P_even = [];
z1 = S0 - 2 * std(S) * sqrt(T - dt);
z2 = S0 + 2 * std(S) * sqrt(T - dt);
z = (z2-z1);
for j = z1:(z2-z1)/15:z2
X_even = [X_even ; X1(j,i)];
C_even = [C_even ; C1(j,i)];
P_even = [P_even ; P1(j,i)];
end
X1_even = [X1_even X_even];
C1_even = [C1_even C_even];
P1_even = [P1_even P_even];
end

 Accepted Answer

You need to do something like this:
vj = z1:(z2-z1)/15:z2;
for j = 1:length(vj)
X_even = [X_even ; X1(j,i)];
C_even = [C_even ; C1(j,i)];
P_even = [P_even ; P1(j,i)];
end
If you are using ‘vj’ in calculations (although you do not seem to be in the code you posted), refer to fractional ‘j’ as ‘vj(j)’.

5 Comments

Thanks a lot!
As always, my pleasure!
Mr. Strider If I may ask a similar question. I created a "for Loop" that worked up to the point where I tried to modify within the "For Loop". If I may I am going to post my code. My error begins on my third for loop when I add .1 to my stress0.
formula Im using if it helps explain my question.
clc
clear all
close all
E1=4000;
E2=5000;
Mu= 1.2*10^5;
stress0.norm = 800;
%The Following values are used to answer part E I
stress0.plus10 = 800+.1;
stress0.minus10 = 800-.1;
% The following loop generates points with E1 and E2.
for i=1:stress0.norm
strain.normal(i)=i/E1;
strain.E1plus10(i)=i/(E1+.1);
strain.E1minus10(i)=i/(E1-.1);
strain.E2plus10(i)=i/E1;
strain.E2minus10(i)=i/E1;
end
for i=stress0.norm:1500
strain.normal(i)=(i/E1)+((i-stress0.norm)/E2);
strain.E1plus10(i)=(i/E1+.1)+((i-stress0.norm)/E2);
strain.E1minus10(i)=(i/E1-.1)+((i-stress0.norm)/E2);
strain.E2plus10(i)=(i/E1)+((i-stress0.norm)/(E2+.1));
strain.E2minus10(i)=(i/E1)+((i-stress0.norm)/(E2-.1));
end
% Creating a another seperated loop for changes in stress0
for i=1:stress0.plus10
strain.stress0plus10(i)=i/E1; % This is where my error begins
end
for i=stress0.plus10;1500
strain.stressplus10(i)=(i/E1)+((i-stress0.plus10)/E2); % This is where my error begins
end
There's no such thing as element 799.9 of an array, like you're trying to assign to in this for loop.
for i=stress0.plus10;1500
strain.stressplus10(i)=(i/E1)+((i-stress0.plus10)/E2); % This is where my error begins
end
The semicolon in the line with your for loop should probably be a colon, but that won't save you because the first iteration through the loop you're trying to assign to strain.stressplus10(799.9).
Als, that should be a new Question. It has nothing directly to do with the current thread.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 13 Jun 2017

Commented:

on 14 Apr 2020

Community Treasure Hunt

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

Start Hunting!