"Array indices must be positive integers or logical values".Error in (line 17) tij_lm(l, m) = min([i(l + 1) - i(l), i(l) - i(l - 1), j(m + 1) - j(m), j(m) - j(m - 1)]) / 2;

1 view (last 30 days)
Getting "Array indices must be positive integers or logical values". Error
Error in cross_correlation (line 17)
tij_lm(l, m) = min([i(l + 1) - i(l), i(l) - i(l - 1), j(m + 1) - j(m), j(m) - j(m - 1)]) / 2;
Heres my code, why do I get the error?
% Example data for time series i and j
i = [1, 2, 3, 4, 5];
j = [1.5, 3, 4.5];
% Call the cross_correlation function with the example data
[Cxy, Jij, tij_lm] = cross_correlation(i, j);
function [Cxy, Jij, tij_lm] = cross_correlation(i, j)
% Inputs:
% i - time series data for variable i
% j - time series data for variable j
% Compute the length of time series i and j
Si = length(i);
Sj = length(j);
% Initialize Jij and tij_lm matrices
Jij = zeros(Si, Sj);
tij_lm = zeros(Si, Sj);
% Compute Jij and tij_lm for each l and m
for l = 1:Si
for m = 1:Sj
tij_lm(l, m) = min([i(l + 1) - i(l), i(l) - i(l - 1), j(m + 1) - j(m), j(m) - j(m - 1)]) / 2;
if 0 < i(l) - j(m) && i(l) - j(m) < tij_lm(l, m)
Jij(l, m) = 1;
elseif i(l) == j(m)
Jij(l, m) = 0.5;
else
Jij(l, m) = 0;
end
end
end
% Compute the cross correlation value C(x/y)
Cxy = sum(sum(Jij));
end
  2 Comments
Dyuman Joshi
Dyuman Joshi on 11 Feb 2023
Indexing in MATLAB starts with 1
When l and m are 1, l-1 and m-1 are 0, which throws the error you are receiving.
""Array indices must be positive integers ...
for l = 1:Si
for m = 1:Sj
tij_lm(l, m) = min([i(l + 1) - i(l), i(l) - i(l - 1), j(m + 1) - j(m), j(m) - j(m - 1)]) / 2;
Since I don't know what you want to do with this line of code, I can't suggest any changes.
Arif Hoq
Arif Hoq on 11 Feb 2023
i = [1, 2, 3, 4, 5];
j = [1.5, 3, 4.5];
l=1:5;
i(l) - i(l - 1)
when l=1
i(1)=1
i(1-1)= i(0) % which shows an error.
also same here
j(m) - j(m - 1)
index should be an integer.
Can you please explain what you want here? please give an example.
tij_lm(l, m) = min([i(l + 1) - i(l), i(l) - i(l - 1), j(m + 1) - j(m), j(m) - j(m - 1)]) / 2;

Sign in to comment.

Accepted Answer

Arif Hoq
Arif Hoq on 11 Feb 2023
if you start your index from 2 then, this can be one approach:
% Example data for time series i and j
i = [1, 2, 3, 4, 5];
j = [1.5, 3, 4.5];
% Call the cross_correlation function with the example data
[Cxy, Jij, tij_lm] = cross_correlation(i, j)
Cxy = 0.5000
Jij = 5×3
0 0 0 0 0 0 0 0.5000 0 0 0 0 0 0 0
tij_lm = 5×3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
function [Cxy, Jij, tij_lm] = cross_correlation(x, y)
% Inputs:
% i - time series data for variable i
% j - time series data for variable j
% Compute the length of time series i and j
xlength = length(x);
ylength = length(y);
% Initialize Jij and tij_lm matrices
Jij = zeros(xlength, ylength);
tij_lm = zeros(xlength, ylength);
% Compute Jij and tij_lm for each l and m
for i = 2:xlength
for j = 2:ylength
tij_lm(i, j) = min([x(i) - x(i), x(i) - x(i - 1), y(j) - y(j), y(j) - y(j - 1)]) / 2;
if 0 < x(i) - y(j) && x(i) - y(j) < tij_lm(i, j)
Jij(i, j) = 1;
elseif x(i) == y(j)
Jij(i, j) = 0.5;
else
Jij(i, j) = 0;
end
end
end
% Compute the cross correlation value C(x/y)
Cxy = sum(sum(Jij));
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!