Array indices must be positive integers or logical values.

I keep getting issues saying 'Array indices must be positive integers or logical values', regarding the line ' c(1:(length(c)+1)/2)=[];' in my code. Im struggling to resolve this, can anybody help?
for i = 1:4
if i == 1
res = sqrt(vel(:,1).^2 +vel(:,2).^2 + vel(:,3).^2);
titles = 'resultant GT';
end
if i == 2
res = vel(:,1);
titles = 'u GT';
end
if i == 3
res = vel(:,2);
titles = 'v GT';
end
if i == 4;
res = vel(:,3);
titles = 'w GT';
subplot(4,1,i)
c = smooth(xcorr(res-mean(res)),1800);
c(1:(length(c)+1)/2)=[];
time = find(c<0,1)
plot(c)
xlabel('time')
ylabel('cor')
hold on
plot([1,length(c)],[0,0],'r')
plot([time,time],[min(c),max(c)],'r')
title([titles, ' ,IT - ', num2str(time)])
end
end

댓글 수: 3

Your code works for me with madeup data. Please share the full error message (all the red text). It would also be helpful if you shared what your variable vel is. You can save that to a mat file and attach it to your post usign the paperclip icon.
C.G.
C.G. 2021년 9월 27일
편집: C.G. 2021년 9월 27일
I have attached the file and the error.
my error is:
Array indices must be positive integers or logical values.
That file is not helpful unfortunately. Can you attached autocorr_test.mat instead?

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

답변 (2개)

G A
G A 2021년 9월 27일

0 개 추천

if length(c) is an even number, then (length(c)+1)/2 is not an integer
Cris LaPierre
Cris LaPierre 2021년 9월 27일
편집: Cris LaPierre 2021년 9월 27일
Are you getting a warning instead of an error? If so, this means the code still runs, but MATLAB is warning of possible ways to improve your code.
There is a slightly more detailed error message in R2021b. Even though the result of the colon operator is integers in this case, MATLAB prefers the inputs to also be integers when the result is used for indexing, as that is more robust.
c=linspace(0,1,8);
length(c)
ans = 8
1:(length(c)+1)/2 % result is integers
ans = 1×4
1 2 3 4
c(1:(length(c)+1)/2) % Warning that inputs to colon operator should also be integers
Warning: Integer operands are required for colon operator when used as index.
ans = 1×4
0 0.1429 0.2857 0.4286

카테고리

도움말 센터File Exchange에서 Scripts에 대해 자세히 알아보기

질문:

2021년 9월 27일

편집:

2021년 9월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by