## Subscript indices must either be real positive integers or logicals.

### Dan (view profile)

on 29 Sep 2013
Latest activity Commented on by azure roy

on 3 Jun 2016

### Matt J (view profile)

```%Number of components
K = zeros(1,111);
```
```%sampling frequency/timing interval
fs = 8000;
Fs = 10*fs;
T = 2;
```
```%frequency components
fk =100:30:3400;
```
```%amplitude components
Ak = rand(1,length(K));
```
```%phase components
phik = 2*pi*rand(1,length(K));
```
```for t = 0:(1/Fs):T %time interval
K = Ak.*cos(2*pi.*fk*t + phik);
y = t/(1/Fs)+1;
xt(1,y) = sum(K,2);
end
```

I'm trying to run this code but for some reason there is an error at y = 14 which is no where near the end of the loop. I'm pretty sure that xt(1,14) is a valid index. Any reason why this might be happening?

## Products

No products are associated with this question.

on 29 Sep 2013
Edited by Matt J

### Matt J (view profile)

on 29 Sep 2013

y will not be 14 exactly because you use non-integer arithmetic to generate it. Using round() might be a possible fix.

on 29 Sep 2013
Edited by dpb

### dpb (view profile)

on 30 Sep 2013

The answer is in the following--

```>> fs = 8000;
>> Fs = 10*fs;
>> t=0:1/Fs:2;
>> y = t/(1/Fs)+1;
>> all(fix(y)==y)
ans =
0
>> neq=find(fix(y)~=y);
>> neq(1)
ans =
14
>> y(14)-fix(y(14))
ans =
1.7764e-15
>> length(neq)
ans =
39209
>> length(y)
ans =
160001
>>
```

Moral: Don't use FP calculations as indices...

Dan

### Dan (view profile)

on 29 Sep 2013

Hmm where does that discrepancy in y values come from?

dpb

on 30 Sep 2013

FP rounding...

### Wayne King (view profile)

on 29 Sep 2013
Edited by Wayne King

### Wayne King (view profile)

on 29 Sep 2013

I'm not sure what you are trying to do, create a superposition of sine waves with different frequencies and a random phase and amplitudes? If so, I'll show you how to do that.

But your immediate issue here is that y=0 when t = 0 and then you try to access the zero-th column of xt(), but that can't exist.

You can do this.

```Fs = 8000;
t = (0:1/Fs:2-1/Fs)';
```
```fk = 100:30:3400;
X = zeros(length(t),length(fk));
```
```phi = -pi+2*pi*rand(1,length(fk));
```
```Ak = rand(1,length(fk));
```
```tmat = repmat(t,1,length(fk));
omega = 2*pi*fk;
omega = repmat(omega,size(tmat,1),1);
phi = repmat(phi,size(tmat,1),1);
freq_phi = omega.*tmat+phi;
Ak = repmat(Ak,size(tmat,1),1);
sigz = Ak.*cos(freq_phi);
sig = sum(sigz,2);
```

Now plot the resulting signal

```plot(t,sig)
```

azure roy

### azure roy (view profile)

on 3 Jun 2016

theta_i(i,j)=2*pi*rand(1) this is showing subscript indices should be integer or logical value. please help with the solution.

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi