26 views (last 30 days)

I am writing a program to simulate an F1 car laptime. Imagibe the car has come out of a corner and is accelerating doen a straight until it has to brake for the next corner. The times for this acceleration and deceleration have to be counted seperately as they are governed by seperate functions. There is a part of the code where the time taken to accelerate on a staright (t1) has to be reset to 0 to then start the the clock for the time taken to decelerate for the upcoming corner (t2) to be counted. I keep getting the same error "Array indices must be positive integers or logical values." no matter what i do.

% Calculate the straights

strTimes = zeros(num_str,1);

sLap = zeros(nseg*nds,1); %sLap = distance around lap

vCar = zeros(nseg*nds,1); %vCar = velocity of car around lap

str = 1;

smax = 0;

for i=1:nseg

rad = track(i,2);

dist = track(i,1);

s = linspace(0,dist,nds)'; %nds = 500 (no. of iterations)

nstart = ((i-1)*nds+1);

nend = (i*nds);

sLap(nstart:nend) = smax + s;

smax = max(sLap);

% Straight

if rad == 0

ds = s(2)-s(1);

% Corner segment numbers

pcsn = i-1; %previous corner segment number

ncsn = i+1; %next corner segment number

if pcsn < 1

pcsn = max(cnrNum);

end

if ncsn>nseg

ncsn = min(cnrNum);

end

pcn = find(pcsn==cnrNum);

ncn = find(ncsn==cnrNum);

[Va,P,G,WE,t1] = calc_acceleration(nds,rho,cd,A,m,s,Rw,cnrVel(pcn));

[Vd,t2] = calc_deceleration(ds,cnrVel(ncn),nds,rho,A,cd,m,cz);

[V,I] = min([Va,Vd],[],2);

vCar(nstart:nend) = V;

% Reset the clock for the braking time, ncn = next corner number

%cnrVel = corner velocity, t1 = acceleration time

%t2 = deceleration time

if max(V) > cnrVel(ncn)

ta = t1(I==3);

tb = t2(I==3);

tb = tb+(ta(end)-tb(1));

t = [ta;tb];

else

t = t1;

end

strTimes(str) = max(t);

str = str+1;

else

vCar(nstart:nend) = ones(nds,1)*cnrVel(i==cnrNum);

end

end

Guillaume
on 11 Apr 2019

They're not meant to be empty

Well, then you go back through the code to find out why they're empty.

Going up we see,

ta = t1(I==3);

So clearly if I doesn't contain 3 exactly, ta will be empty. So what is I?

[V,I] = min([Va,Vd],[],2);

It's the colum index where a minimum is found. I don't know what Va and Vd are. if they are column vectors then [Va, Vd] only has two columns, and obviously, you're never going to find a minimum in the 3rd column. If Va and or Vd have more than one column, the the minimum could be found in column 3 but there is absolutely no guarantee of that.

As it is, either you're guaranteed they're empty (if Va and Vb both have only one column) or they could be empty if no row of [Va, Vb] has its minimum in the 3rd column.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692642

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692642

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692644

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692644

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692646

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692646

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692647

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692647

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692651

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692651

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692657

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/455729-array-indices-must-be-positive-integers-or-logical-values#comment_692657

Sign in to comment.