why showing 'Array indices must be positive integers or logical values.'

1 次查看(过去 30 天)
m2 = zeros(N,1); %2Matrix of second moment matrix
m2_pul = zeros(N,1); %x(n)x(n+t)Matrix frame
fs = 50;
for t1 = (0:N-1)/fs
n = 1:N;
m2_pul(n) = sig(n) .* sig(n+t1); %x(n)x(n+t)
m2(t1+1) = mean(m2_pul); %E[x(n)x(n+t)]
m2_pul = zeros(N,1);
end
Array indices must be positive integers or logical values.
Error in bispectrum (line 12)
m2_pul(n) = sig(n) .* sig(n+t1); %x(n)x(n+t)
Error in bi_estimation (line 102)
[c3_w]=bispectrum(sig,sig2,N);

采纳的回答

KSSV
KSSV 2020-1-28
编辑:KSSV 2020-1-28
m2 = zeros(N,1); %2Matrix of second moment matrix
m2_pul = zeros(N,1); %x(n)x(n+t)Matrix frame
fs = 50;
f = (0:N-1)/fs ;
for t1 = 1:length(f)
n = 1:N;
m2_pul(n) = sig(n) .* sig(n+t1); %x(n)x(n+t)
m2(t1+1) = mean(m2_pul); %E[x(n)x(n+t)]
m2_pul = zeros(N,1);
end
The indices of array in MATLAB cannot be zero, negative and fractions. When you take:
t1 = (0:N-1)/fs
t1 has fractions, you cannot use them as indices; so error.
  5 个评论
Walter Roberson
Walter Roberson 2020-1-28
n = 1:N;
Okay, everything in n is going to be a positive integer. It will have entries like [1, 2, 3, 4, 5, ...]
for t1 = (0:N-1)/fs
That would be, for example, [0/8000, 1/8000, 2/8000, ....] which is going to be mostly non-integer. It will be integer at 0 and at each exact integer multiple of fs .
m2_pul(n) = sig(n) .* sig(n+t1); %x(n)x(n+t)
The first sub-expression of that is sig(n) . With n being all positive integers and sig being an array, then sig(n) is valid indexing provided that the maximum value in n does not exceed the number of elements in sig .
The second sub-expression is sig(n+t1) . n has entries like [1, 2, 3, 4, 5, ...] and t1 has entries like [0/8000, 1/8000, 2/8000, ...] and so on, so n+t1 is like [1+0/8000, 2+1/8000, 3+2/8000, 4+3/8000], and so on. The first of those will be an integer, and at one place further than exact integer multiplies of fs, there will be integers (so like 1, 8001, 160001) but most of n+t1 will be non-integer. Those are invalid indices.
But KSSV's suggestion of
f = (0:N-1)/fs ;
for t1 = 1:length(f)
n = 1:N;
m2_pul(n) = sig(n) .* sig(n+t1); %x(n)x(n+t)
is successful in avoiding using the n+((0:N-1)/fs) as **indices* . It also happens not to use the content of (0:N-1)/fs which might or might not be a problem.
I cannot tell what the original code is intended to do, so I am not sure what to suggest.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by