Problem with error "Subscript indices must either be real positive integers or logicals"
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
I know this is a common problem, but I don't know how to fix it in my own context. I get the following error on line 20 (Y = y(Ty)): "Subscript indices must either be real positive integers or logicals". I don't think I'm using an in-built function, and I checked Ty and y to see if there are any 0s, but there are none. I find it really strange as the code for "y" is the same as "x", but I'm only getting this error with "y". I am using Matlab R2017a.
My code:
clear all
close all
x = rand(1,38400);
fs = 128;
t = (0:length(x)-1)/fs;
ST = 10*fs;
EN = 60*fs;
T = t(ST:EN);
Tx = T*fs;
X = x(Tx);
y = rand(1,75000);
fs1 = 250;
t1 = (0:length(y)-1)/fs1;
ST1 = 10*fs1;
EN1 = 60*fs1;
T1 = t1(ST1:EN1);
Ty = T1*fs1;
Y = y(Ty); %Line 20
newyfs = fs1/fs;
ny = Y(1:newyfs:end);
nt = T1(1:newyfs:end);
nfs1 = fs1/newyfs;
[acor,lag] = xcorr(ny,X,'coef');
[~,I] = max(abs(acor));
timeDiff = lag(I);
figure
subplot(311); plot(Tx,X); title('X');
subplot(312); plot(nt,ny); title('Y');
subplot(313); plot(lag/nfs1,acor);
title('Cross-correlation')
I am practising this code so I can apply it to my data, where I want to check the correlation of two signals from the same source but each signal is recorded from different systems.
0 件のコメント
採用された回答
Adam
2018 年 5 月 1 日
Use
Y = y( round( Ty ) );
to ensure they are actually integers. at least one of them must have decimal values, however small.
3 件のコメント
Adam
2018 年 5 月 2 日
isinteger tests whether the data is of integer type, not whether the variable is integer valued. Anything that is the result of maths involving operations more complicated than addition and subtraction may result in data that is not quite integer valued though, even if you start with integers and the exact analytic result should be an integer - it's just the way floating point maths works on a computer.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Transforms についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!