MATLAB Answers

0

Array indices must be positive integers or logical values

Asked by Brooks Corbett on 17 Nov 2019 at 20:25
Latest activity Edited by Star Strider
on 17 Nov 2019 at 20:45
H = 0;
M = length(h);
for k=0:M-1
H = H + (h(k) * exp(-1i*2*pi*fd));
end
%fd and h come from a function call

  0 Comments

Sign in to comment.

2 Answers

Answer by Star Strider
on 17 Nov 2019 at 20:39
Edited by Star Strider
on 17 Nov 2019 at 20:45
 Accepted Answer

The problem:
for k=0:M-1
The solution:
for k=1:M
In MATLAB, subscript indices begin at 1, not 0, and are defined as integers greater than 0.
It is more efficient to do this without the loop:
H = cumsum(h* exp(-1i*2*pi*fd));
or simply:
H = sum(h* exp(-1i*2*pi*fd));

  0 Comments

Sign in to comment.


Answer by Fabio Freschi on 17 Nov 2019 at 20:39

Your index k starts from 0 and you are indexing h with k, so you are asking at the first iteration h(0). Matlab has 1-indexing so you get the error. I guess your for loop should be
for k = 1:M
...
end

  0 Comments

Sign in to comment.