Jesus
Jesus

Reputation: 179

How to store complex data into an array using a For loop?

I am trying to store data that contains complex numbers into an array. I am having issues on how to setup the loop and extract the data. The error I am getting says "Subscript indices must either be real positive integers or logicals" which is in code line 11.

for k=10e-10:0.01:10  %discrete reduced frequency range
Ck = (besselh(1,2,k))./(besselh(1,2,k)+1i*besselh(0,2,k)); %Bessel function

%Matrices
A=[0.8132  -0.1008; -0.0725  2.0518]; 
B=Ck*[7.623  57.15; -8.233  -57.157]; 
C=Ck*[1865  1473.14; -1119  11907.48]; 

%frd function
Hresp=frd(Abar,k);
H11(k)=Hresp;
end

I would like my output data to look like the image below: I would like my output data to look like this

Upvotes: 0

Views: 66

Answers (2)

AnonSubmitter85
AnonSubmitter85

Reputation: 933

The value of k is a non-integer and you are trying to index with it. There is no 0.1 element of an array, only elements 1, 2, 3, ... Use a separate variable to keep track of what index you are on. For instance,

ind = 1;
for k = 10e-10 : 0.01 : 10
  % some work would go here
  H11(ind) = whatever;
  ind = ind + 1;
end

Upvotes: 0

Cris Luengo
Cris Luengo

Reputation: 60635

The simplest way (IMO) to do what you want is to have an integer loop index, and a pre-computed array with k values:

k = 10e-10:0.01:10;
H11 = zeros(size(k));
for ii = 1:numel(k)
  H11(ii) = k(ii); % whatever computation here involving k(ii)
end

Note that I pre-allocated H11, this prevents re-allocation during the loop execution.

Upvotes: 1

Related Questions