0

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

2
  • You failed to specify what the issue actually is. I am guessing that you are getting a complain about a non-integer index from H11(k). Commented Jul 16, 2018 at 23:10
  • Thanks, I will include that. & Yes, that is the issue Commented Jul 16, 2018 at 23:13

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

This does not work with the frd function... it complains "subscripted assignment dimension mismatch"
@Jesus you still got to write k=10e-10:0.01:10 instead of k = 0 : 0.1 : 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.